测试你是否和LTC水平一样高
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10092 Accepted Submission(s): 3254
Problem Description
大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!
你的任务是:
计算方程x^2+y^2+z^2= num的一个正整数解。
你的任务是:
计算方程x^2+y^2+z^2= num的一个正整数解。
Input
输入数据包含多个测试实例,每个实例占一行,仅仅包含一个小于等于10000的正整数num。
Output
对于每组测试数据,请按照x,y,z递增的顺序输出它的一个最小正整数解,每个实例的输出占一行,题目保证所有测试数据都有解。
Sample Input
3
Sample Output
1 1 1
Author
lcy
Source
Recommend
Ignatius.L
暴力大家都会,这里用hash
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int hash[10010],res[110]; void Init(){ memset(hash,0,sizeof(hash)); for(int i=1;i<=100;i++){ res[i]=i*i; hash[res[i]]=1; } } int main(){ //freopen("input.txt","r",stdin); int num; Init(); while(~scanf("%d",&num)){ int flag=1; for(int i=1;i<=100 && flag;i++) for(int j=1;flag && res[i]+res[j]<num;j++){ int tmp=num-res[i]-res[j]; if(hash[tmp]){ printf("%d %d %d\n",i,j,int(sqrt(tmp))); flag=0; } } } return 0; }