#15457: 加快運算的方法:如果個位數不等於0, 10位數以上的位值次方和不變(與上一個數相同)


kuku5521 (kuku)

學校 : 不指定學校
編號 : 85863
來源 : [163.22.59.254]
最後登入時間 :
2024-08-28 08:23:19
a040. 阿姆斯壯數 | From: [49.215.176.7] | 發表日期 : 2018-10-06 19:42

#include<iostream>
#include <cmath>

using namespace std;


int main()
{
int i = 0, j = 0, k = 0, myLower, myUpper, pv[8], myPower = 0, myCounter = 0, myCounter2 = 0, myValue = 0, myPrev = 0;


while(cin >> myLower >> myUpper)
{
myCounter2 = 0;
for (i = myLower; i <= myUpper; i++) //由下限往上找
{
myCounter = 0; //位數個數
myPower = 1; //pow(10,0)
myValue = 0;
for (j = 1; j < 7; j++)
{
myPower *= 10;
pv[j] = (i % myPower) / (myPower / 10);
myCounter++;
if (i % myPower == i)break;
}
if (pv[1] == 0 || i == myLower)
{
for (k = 1; k <= myCounter; k++)myValue += pow(pv[k], myCounter);
myPrev = myValue - pow(pv[1], myCounter);
}
else
{
myValue = myPrev + pow(pv[1], myCounter); //上一個數的位值次方和(不含個位數)
}
if (myValue == i)
{
cout << i << " ";
myCounter2++;
}
}
if (myCounter2 == 0)cout << "none";
cout << endl;
}
return 0;
}

 
ZeroJudge Forum