解题思路:3循环和1对比
注意事项:4循环过不去,3个可以
参考代码:
正确
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
int main()
{
int n,a,b,c,d;
cin >> n;
for ( a = 0; a * a <= n / 4; a++)
for ( b = a; b * b <= n / 3; b++)
for ( c = b; c * c <= n / 2; c++)
{
d = sqrt(n - a * a - b * b - c * c);
if (d * d == n - a * a - b * b - c * c)
{
cout << a << " " << b << " " << c << " " << d << endl;
return 0;
}
}
}
错误1
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
int main()
{
int a,i,j,k,l;
cin>>a;
for(i=0; i<=sqrt(a)+1; i++)
for(j=i; i<=sqrt(a)+1; j++)
for(k=j; k<=sqrt(a)+1; k++)
for(l=k; l<=sqrt(a)+1; l++)
{
if(i*i+j*j+k*k+l*l==a)
{
cout<<i<<" "<<j<<" "<<k<<" "<<l<<endl;
break;
}
}
}
错误2
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
int main()
{
int a,t1,t2,t3,t4;
cin>>a;
t4=sqrt(a);
a=(int)(a-t4*t4);
t3=sqrt(a);
a=(int)(a-t3*t3);
t2=sqrt(a);
a=(int)(a-t2*t2);
t1=sqrt(a);
a=(int)(a-t1*t1);
printf("%d %d %d %d",t1,t2,t3,t4);
}
错误2可以用777
0.0分
1 人评分