杭电ACM2010:水仙花数

  • Post author:
  • Post category:其他


#include <iostream>
using namespace std;
int main(){
    int m, n;
    int a, b, c,flag=0;
    int x[1000];
    while (cin >> m >> n)
    {
        if (m > n){
            n = m + n;
            m = n - m;
            n = n - m;
        }
        for (int i = m; i <= n; i++){
            a = i % 10; b = (i / 10) % 10; c = i / 100;
            if (i == a*a*a + b*b*b + c*c*c){
                x[flag] = i;
                flag++;
            }
        }
        if (flag == 0) cout << "no" << endl;
        else{
            for (int i = 0; i < flag-1; i++)  //因为从0开始
                cout << x[i] << " ";
            cout << x[flag - 1] << endl;      //flag-1为最后一位
        }
        flag = 0;
    }
    return 0;
}

格式话输出是AC的关键



版权声明:本文为u012239518原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。