1. 程式人生 > >2019迅雷筆試

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;
}

 程式碼通過哈