第二期训练第五题(HDU-2010)

问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2010

问题简述:输入多组两个三位数的数字,求在这两个数字范围内符合各位数字的立方和等于其本身的数。并将这些数按序输出。如果没有这样的数字,则输出“no”。

Get:(1)求某个数的幂用:pow((数字),(幂)) (这个函数在头文件<cmath>中,函数原型为 double pow(double x, double y)
(2)用sort函数实现按序输出 (这个函数在头文件<algorithm>中)

AC代码:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int m, n,i,a,b,c;
    while (cin >> m >> n)
    {
        int s = 0,d[900] = { 0 };
        for (i = m; i < n + 1; i++)
        {
            a = i % 10;
            b = (i / 10) % 10;
            c = (i / 100) % 10;
            if (i == pow(a, 3) + pow(b, 3) + pow(c, 3))
            {
                d[s] = i;
                s++;
            }
        }
        if (d[0])
        {
            sort(d, d + s - 1);
            for (i = 0; i < s-1; i++)
            {
                cout << d[i] << " ";
            }
            cout << d[s-1]<<endl;
        }
        else
        {
            cout << "no"<<endl;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43973189/article/details/84996332
今日推荐