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;
                    }
                }
            }
        }
    }
}

 

 

 

 

 


 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dllglvzhenfeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值