题解
首先把100到1000中的水仙花数房间一个数组,然后输入m和n,再利用循环输出在m和n之间的水仙花数,值得注意的是,第一次只输出一个水仙花数,后面的每一个都要输出一个空格和一个水仙花数,如果直接输出一个水仙花数加一个空格,会出现PE错误。
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int m, n, i, x, y, z, s,t;
int p[1001] = { 0 };
while (cin >> m >> n)
{
t = 0;
s = 1;
for (i = m; i < n; i++)
{
x = i / 100;
y = (i - 100 * x) / 10;
z = (i - 100 * x - 10 * y);
if (i == pow(x, 3) + pow(y, 3) + pow(z, 3)) p[i] = i;
}
for (i = m; i <= n; i++)
{
if (p[i] == 0) continue;
else if(t==0) {cout << p[i] ; s = 0; t = 1; }
else { cout << " " << p[i]; s = 0; }
}
if (s==0) cout << endl;
if (s) cout << "no" << endl;
}
}