第二期訓練第五題(HDU-2010)
阿新 • • 發佈:2018-12-22
問題連結: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;
}
}
}