ACM练习题——判断因数

Description
描述:

Today is the National day, and lmdd’s classmates and friends all go out and have fun, While lmdd can do nothing but coding and gaming. He feels bored and decides to play with numbers to kill time.
今天是国庆节,lmdd的同学和朋友们都出去玩了,而lmdd除了编程和游戏什么也做不了。他感到无聊,决定玩数字来消磨时间。

He counts the numbers of divisors of a given positive integer n. For example, 10 has 4 divisors 1, 2, 5, and 10. If the number of divisors of positive integer n is odd, lmdd will feel more lonely. Now lmdd is too sad to count for himself, he asks you for help.
他计算给定正整数n的约数。例如,10有4个约数1、2、5和10。如果正整数n的约数为奇数,lmdd会感到更孤独。现在lmdd太伤心了,他请求你的帮助。

Input

The first line an integer n, indicates n (n≤200) test cases. Then follows n lines, and each line contains one integer x(x<=10^9).
第一行为整数n,表示n (n≤200)测试用例。接下来是n行,每一行包含一个整数x(x<=10^9)。

Output

For each integer x, if the number of divisors of x is even print “YES” in a line. Print “NO” otherwise.

对于每个整数x,如果x的除数是偶数,则在一行中打印“YES”。否则打印“NO”。

Sample Input 1

3
2
3
4
Sample Output 1

YES
YES
NO

这道题的思路是:
只要判断这个输入的数是否是别的数的平方就可以了,因为不是平方的约数都是两两对应的。

#include                 
#include
int main(void){
	int n,i,t;
	int *a;
	a=(int *)malloc(sizeof(int )*100);
	while(scanf("%d",&n) != EOF){
		for(i=0;i<n;i++){
			scanf("%d",&a[i]); 
		}
		for(i=0;i<n;i++){
			t=1;
			while(1){
				if(a[i]==t*t){
					a[i]=0;
					break;
				}
				if(a[i]<t*t){
					break;
				}
				t++;
			}
		}
		for(i=0;i<n;i++){
			if(a[i]==0){
				printf("NO\n");
			}
			else{
				printf("YES\n");
			}
		}
	}
} 

你可能感兴趣的:(C,c语言,acm竞赛)