#1305:Fermat vs. Pythagoras--注意关键变量清零

结果:wrong answer:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int num;
	int m, n, k, i, maxN, x, y, z, a, b, r;
	int count=0, unuse=0;
	bool flag[1000001] = {false};//此处是关键
	while (scanf("%d",&num) != EOF){
		
		count=0, unuse=0;
		for (m=1; m <= (int)sqrt(num-1); m++){
			maxN = (int)sqrt(num - m*m);
			maxN = maxN >= m ? m-1 : maxN;
//printf("\n\nm=%d, maxN=%d\n", m, maxN);
			for (n=1; n <= maxN; n++){
//printf("\n\n@n=%d\n",n);
				//辗转相除法
				if (n%2 != m%2){
				   a=m, b=n, r=1;
				   for(; (r=a%b) != 0; a=b, b=r){
//printf("a=%d, b=%d, r=%d\n", a, b, r);
					;
				   }
				   if (b==1){
                      count ++;
//printf("count=%d\n", count);
				      x = m*m - n*n;
				      y = 2*m*n;
				      z = m*m + n*n;
//printf("x=%d, y=%d, z=%d\n", x, y, z);
                      for (k=1; k*z <= num; k++){
					      flag[k*x] = flag[k*y] = flag[k*z] = true;
//printf("k=%d k*z=%d\n", k, k*z);
					  }
				   }
				}
			}
		}

//		printf("%d ", count);

		for(i=1; i<= num; i++)
			if (!flag[i])
				unuse ++;
		printf("%d %d\n", count, unuse);
	} //while

	return 0;
}

结果:AC

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int num;
	int m, n, k, i, maxN, x, y, z, a, b, r;
	int count=0, unuse=0;
//	bool flag[1000001] = {false};
	while (scanf("%d",&num) != EOF){
		bool flag[1000001] = {false};//发现区别了吗?
		count=0, unuse=0;
		for (m=1; m <= (int)sqrt(num-1); m++){
			maxN = (int)sqrt(num - m*m);
			maxN = maxN >= m ? m-1 : maxN;
//printf("\n\nm=%d, maxN=%d\n", m, maxN);
			for (n=1; n <= maxN; n++){
//printf("\n\n@n=%d\n",n);
				//辗转相除法
				if (n%2 != m%2){
				   a=m, b=n, r=1;
				   for(; (r=a%b) != 0; a=b, b=r){
//printf("a=%d, b=%d, r=%d\n", a, b, r);
					;
				   }
				   if (b==1){
                      count ++;
//printf("count=%d\n", count);
				      x = m*m - n*n;
				      y = 2*m*n;
				      z = m*m + n*n;
//printf("x=%d, y=%d, z=%d\n", x, y, z);
                      for (k=1; k*z <= num; k++){
					      flag[k*x] = flag[k*y] = flag[k*z] = true;
//printf("k=%d k*z=%d\n", k, k*z);
					  }
				   }
				}
			}
		}

//		printf("%d ", count);

		for(i=1; i<= num; i++)
			if (!flag[i])
				unuse ++;
		printf("%d %d\n", count, unuse);
	} //while

	return 0;
}

特此记录!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值