[C++]水仙花数

[C++]水仙花数

题目

水仙花数(daffodil)
输出100~999中的所有水仙花数。若3位数ABC满足ABC=A3 + B3+C3,则称其为水仙花数。例如153=13+53+33,所以153是水仙花数。

代码实现

三个for循环遍历一遍,满足条件输出。
#include <iostream>

using namespace std;

int main()
{
    int a, b, c, temp;
    for(a = 1; a <= 9; a++)
    {
        for(b = 0; b <= 9; b++)
        {
            for(c = 0; c <= 9; c++)
            {
                temp = a * 100 + b * 10 + c;
                if(temp == a * a * a + b * b * b + c * c * c)
                    cout<<temp<<endl;
            }
        }
    }
    return 0;
}

发布了19 篇原创文章 · 获赞 11 · 访问量 2426

猜你喜欢

转载自blog.csdn.net/Cedric_6/article/details/104109089