11565:Simple Equations
問題文
http://uva.onlinejudge.org/external/115/11565.html
A,B,Cが与えられる。
x+y+z=A
xyz=B
x^2+y^2+z^2=C
以上の3つの条件を同時に満たすような
辞書順最小の3つの異なる整数(x,y,z)を求めよ。
整数なのでマイナスも含まれる。
xyz=B B<=10000 だから
x,y,zそれぞれについて√Bまで調べればよい。
#include<iostream> #include<algorithm> using namespace std; int a,b,c; void solve(){ for(int x=-100;x<101;x++){ for(int y=x+1;y<101;y++){ for(int z=y+1;z<101;z++){ if(x+y+z==a && x*y*z==b && x*x+y*y+z*z==c){ cout << x << " " << y << " " << z << endl; return ; } } } } cout << "No solution." << endl; } int main(void){ int n; cin >> n; while(n--){ cin >> a >> b >> c; solve(); } return 0; }