经典水仙花数

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了经典水仙花数脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1³+5³+3³ 我们一般的写法:

#include <iostream>
#include <cstring>
#include <algorIThm>

using namespace std;

int main(){
	for(int i = 100; i <= 999; i++){
		int digit = i % 10;	//个位
		int tenDigit = i / 10 % 10;	//十位 
		int threeDigit =  i / 100;	//百位
		if(i == digit * digit * digit + tenDigit * tenDigit * tenDigit + threeDigit * threeDigit * threeDigit){
			cout << i << endl;
		} 
	} 
    return 0;

相对数字拆分来说,数字组合的效率高一丶丶。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main(){
    //i是百位数,j是十位数,k是个位数
    for(int i = 1; i <= 9; i++){
        for(int j = 0; j <= 9; j++){
            for(int k = 0; k <= 9; k++){
                int total = i * 100 + j * 10 + k;
                if(i * i * i + j * j * j + k * k * k == total){
                    cout << total << endl; 
                }
            }
        }
    }
    return 0;
}

脚本宝典总结

以上是脚本宝典为你收集整理的经典水仙花数全部内容,希望文章能够帮你解决经典水仙花数所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。