针对这个ACM题目
http://uva.onlinejudge.org/external/115/11565.html
代码如下:
#include<iostream>
using namespace std;
int main()
{
int N,A,B,C;
int x,y,z;
cin>>N;
do
{
S: cin>>A>>B>>C;
for(int j = 1;j < A;++j)
{
for(int k = 1;k < A;++k)
{
for(int n = 1;n< A;++n)
{
if((j + k + n == A)&&(j*k*n == B)&&(j*j + k*k +n*n == C))
{
x = j;
y = k;
z = n;
//cout<<x<" "<<y<<" "<<z<<endl;
printf("%d %d %d",x,y,z);
goto S;
}
}
}
}
cout<<"No Solution"<<endl;
}while(N-->1);
return 0;
}
感觉写的有点笨笨的,还有哦cout<<x<" "<<y<<" "<<z<<endl;这句有什么问题吗?用LUNIX编译一直通不过的
前面的代码是针对x y z大于0的情况,如果其小于0,代码如下:
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int main ( int argc, char* argv [] ) {
-
-
int A, B, C;
-
int x, y, z;
-
int count, found;
-
-
scanf ( "%d", &count );
-
while ( count-- > 0 ) {
-
-
scanf ( "%d %d %d", &A, &B, &C );
-
-
if ( A < 1 || B < 1 || C < 1 || A > 10000 || B > 10000 || C > 10000 ) {
-
continue;
-
}
-
-
found = 0;
-
for ( x = -100; !found && x <= 100; ++x ) {
-
-
if ( 2*x*x*x - 2*A*x*x + (A*A-C )*x - 2*B == 0 ) {
-
-
for ( y = -100; !found && y <= 100; ++y ) {
-
-
if ( x*y*y + x*x*y - A*x*y + B == 0 ) {
-
-
z = A - x - y;
-
-
if ( x != y && y != z && x != z
-
&& x*x + y*y + z*z == C
-
) {
-
found = 1;
-
}
-
-
}
-
-
}
-
-
}
-
-
}
-
-
}
-
-
return 0;
-
}