[解题报告]HDU 1407 测试你是否和LTC水平一样高

测试你是否和LTC水平一样高

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9965    Accepted Submission(s): 3207


Problem Description
大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!
你的任务是:
计算方程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
 

这样居然没有超时。。。。。

#include<stdio.h>
int main()
{
    int n ;
    while(scanf("%d",&n)!=EOF)
    {
        int x,y,z;
        int sumx,sumy,sumz ;
        int flag=1;
        for(x=1;x*x<=n&&flag;x ++)
        {
            sumx=x*x ;
            for(y=1;y*y<=n&&flag;y++)
            {
                sumy=y*y;
                for(z=1;z*z<=n&&flag;z++)
                {
                    sumz=z*z;
                    if(sumx+sumy+sumz==n)
                    {
                        printf("%d %d %d\n",x,y,z);
                        flag=0;
                    }
                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/TheLaughingMan/p/3043517.html