2.1基本算法之枚举_1812完美立方
生活随笔
收集整理的這篇文章主要介紹了
2.1基本算法之枚举_1812完美立方
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://noi.openjudge.cn/ch0201/1812/
C++代碼來源于以下鏈接
https://blog.csdn.net/qq_26919935/article/details/77905491
https://blog.csdn.net/xcdq_aaa/article/details/112630852
/* 1812完美立方(2.1基本算法之枚舉) 2.1基本算法之枚舉_1812完美立方 https://blog.csdn.net/qq_26919935/article/details/77905491 http://noi.openjudge.cn/ch0201/1812/ */ #include <iostream> #include<stdio.h> using namespace std; //http://noi.openjudge.cn/ch0201/1812/ //4重循環,關鍵是該break的時候盡早,省了很多無用功 int n,a,b,c,d,a3,b3,c3,d3; int main(int argc, char *argv[]) {cin>>n;for(int a=2;a<=n;a++){a3=a*a*a;for(int b=2;b<=n;b++){b3=b*b*b;if(b3>=a3)break;for(int c=b;c<=n;c++){c3=c*c*c;if(c3+b3>=a3)break;for(int d=c;d<=n;d++){d3=d*d*d;if(d3+c3+b3>a3)break;if(d3+c3+b3==a3){printf("Cube = %d, Triple = (%d,%d,%d)\n",a,b,c,d);}}}}}return 0; }/* 2.1基本算法之枚舉_1812完美立方 方法二 http://noi.openjudge.cn/ch0201/1812/https://blog.csdn.net/qq_26919935/article/details/77905491 https://blog.csdn.net/xcdq_aaa/article/details/112630852 */ #include <iostream> using namespace std; int all[20]; int main() {int n;cin >> n;for (int i = 2; i <= n; i++) {for (int j = 2; j <= n; j++) {for (int k = j; k <= n; k++) {for (int l = k; l <= n; l++) {if (i * i * i == j * j * j + k * k * k + l * l * l) {cout << "Cube = " << i << ", Triple = (" << j << ","<< k << "," << l << ")" << endl;}}}}} }
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的2.1基本算法之枚举_1812完美立方的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序消息推送自有服务器无法接收到信
- 下一篇: 软考高项信息系统项目管理工程师自考500