Description
a³ = b³ + c³ + d³为完美立方等式。例如 12³ = 6³ + 8³ + 10³。对任意给出的正整数N(N≤100),寻找所有的四元组(a, b, c, d),使得 a³ = b³ + c³ + d³, 其中 1 < a, b, c, d ≤ N。
Input
正整数N(N≤100)
Output
每行输出一个完美立方,按照a的值从小到大依次输出。当两个完美立方等式中a相同,则依次按照b、c、d进行非降序排列输出,即b值小的先输出、然后c值小的先输出、然后d值小的先输出。
Sample Input
24
Sample Output
Cube = 6, Triple = (3,4,5)
Cube = 9, Triple = (1,6,8)
Cube = 12, Triple = (6,8,10)
Cube = 18, Triple = (2,12,16)
Cube = 18, Triple = (9,12,15)
Cube = 19, Triple = (3,10,18)
Cube = 20, Triple = (7,14,17)
Cube = 24, Triple = (12,16,20)
题解
4 重 for 循环
强行模拟即可
代码
/* By:OhYee Github:OhYee Blog:http://www.oyohyee.com/ Email:oyohyee@oyohyee.com かしこいかわいい? エリーチカ! 要写出来Хорошо的代码哦~ */ #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <vector> #include <list> #include <queue> #include <stack> #include <map> #include <set> #include <functional> using namespace std; const int maxn = 1000; bool Do() { int N; if(scanf("%d",&N) == EOF) return false; int a,b,c,d; for(a = 1;a <= N;a++) { int a3 = a*a*a; for(b = 1;b <= N;b++) { int b3 = b*b*b; if(a3 < b3) break; for(c = b;c <= N;c++) { int c3 = c*c*c; if(a3 < b3 + c3) break; for(d = c;d <= N;d++) { int d3 = d*d*d; if(a3 < b3 + c3 + d3) break; if(a3 == b3 + c3 + d3) printf("Cube = %d, Triple = (%d,%d,%d)\n",a,b,c,d); } } } } return true; } int main() { while(Do()); return 0; }