测试你是否和LTC水平一样高
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
错误代码:
尽量的不要用return
# include <iostream>
# include <cstdio>
int main(){
int num;
int flaf = 0;
while(scanf("%d",&num)!=EOF){
for(int i=1;i<=10000;i++)//这里应该写成100不应该是10000,x^2+y^2+z^2= num i<=num这个值
for(int j=i;j<=10000;j++)
for(int k=j;k<=10000;k++)
if(i*i+j*j+k*k==num){
printf("%d %d %d\n",i,j,k);
return 0;
flag = 1;
}
}
return 0;
}
超时的代码:
# include <iostream>
# include <cstdio>
int main(){
int num;
while(scanf("%d",&num)!=EOF){
int flag = 0;
for(int i=1;i<=num;i++){
for(int j=i;j<=num;j++){
for(int k=j;k<=num;k++)
if(i*i+j*j+k*k==num&&j>=i&&k>=j){
printf("%d %d %d\n",i,j,k);
flag = 1;
}
if(flag==1)
break;
}
if(flag==1)
break;
}
}
return 0;
}
提交Accepted(748MS):
# include <iostream>
# include <cstdio>
# include <cmath>
int main(){
int num;
while(scanf("%d",&num)!=EOF){
int flag = 0;
for(int i=1;i<=sqrt(num);i++){
for(int j=i;j<=sqrt(num);j++){
for(int k=j;k<=sqrt(num);k++)
if(i*i+j*j+k*k==num&&j>=i&&k>=j){
printf("%d %d %d\n",i,j,k);
flag = 1;
}
if(flag==1)
break;
}
if(flag==1)
break;
}
}
return 0;
}
错误代码:
# include <iostream>
# include <cstdio>
int main(){
int n,m;
int i,j,k;
while(scanf("%d",&n)!=EOF){
int flag = 0;
for(i=1;i*i<=100;i++){
//这里错误,不要是写i就写i<=100,如果是i*i就是i*i<=n,想想
for(j=i;j*j<=100-i;j++){
for(k=j;k*k<=100-i-j;k++){
if(i*i+j*j+k*k==n){
printf("%d %d %d\n",i,j,k);
flag = 1;
break;
}
}
if(flag)
break;
}
if(flag)
break;
}
}
return 0;
}
正确代码:
# include <iostream>
# include <cstdio>
int main(){
int n,m;
int i,j,k;
while(scanf("%d",&n)!=EOF){
int flag = 0;
for(i=1;i*i<=n;i++){
for(j=i;j*j<=n-i;j++){
for(k=j;k*k<=n-i-j;k++){
if(i*i+j*j+k*k==n){
printf("%d %d %d\n",i,j,k);
flag = 1;
break;
}
}
if(flag)
break;
}
if(flag)
break;
}
}
return 0;
}
提交代码2:
错误代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#define M 100
using namespace std;
int main()
{
int m;
while(scanf("%d", &m) != EOF){
double k = 0;
for(int i=1; i<sqrt(m); i++)
{
for(int j=1; j<sqrt(m); j++)
{
k = sqrt(m - i*i - j*j);
if(!(k- int(k)) && k != 0)
{
printf("%d %d %d\n", i, j, int(k));
goto the;
}
}
}
the:{}
}
return 0;
}
正确代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#define M 100
using namespace std;
int main()
{
int m;
while(scanf("%d", &m) != EOF){
double k = 0;
for(int i=1; i<100; i++)
{
for(int j=1; j<100; j++)
{
k = sqrt(m - i*i - j*j);
if(!(k- int(k)) && k != 0)
{
printf("%d %d %d\n", i, j, int(k));
goto the;
}
}
}
the:{}
}
return 0;
}