欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2019迅雷笔试

程序员文章站 2022-05-14 15:49:03
...

2019迅雷笔试

首先,勾股数的证明:可以参考百度百科-勾股数

代码主要分为两个部分:判断是否互质;判断是否符合勾股数

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace std;


const int M=1000010;
bool Judge[M];
int JugdgeNumber(long a,long b)
{
	return b==0?a:JugdgeNumber(b,a%b);
}

int main()
{
	long a,b,c,N;
	cin>>N;
	int nOut=0;
	long m=(long)sqrt(N+0.5);

	memset(Judge, 0, sizeof(Judge));
	for (long i=1;i<=m;i++)
	{
		for (long j=i+1;j*i<=N;j+=2)
		{
			if (JugdgeNumber(j,i)==1)
			{
				a=i*j*2;  b=(j*j-i*i);  c=(j*j+i*i);
				if (c<=N)
				{
					nOut++;
					if (!Judge[a]) { Judge[a] = 1; }
					if (!Judge[b]) { Judge[b] = 1; }
					if (!Judge[c]) { Judge[c] = 1; }
				}
				for (int k=2;c*k<=N;k++)
				{
					if (!Judge[a * k]) { Judge[a * k] = 1; }
					if (!Judge[b * k]) { Judge[b * k] = 1; }
					if (!Judge[c * k]) { Judge[c * k] = 1; }
				}
			}
		}
	}
	cout<<nOut<<endl;
	return 0;
}

 代码通过哈