#29566: c++ 紀錄


e3524167 (Kenlogin)

學校 : 不指定學校
編號 : 186743
來源 : [182.233.207.92]
最後登入時間 :
2022-03-17 21:12:56
a040. 阿姆斯壯數 | From: [182.233.207.92] | 發表日期 : 2022-03-12 14:54

C++14 結果錯誤 紀錄


#include <iostream>

#include <string> 

#include <sstream>

 

using namespace std;

int pow(int a, int b);

int armstrong(int a, int b);

int main()

{

string a, b;

while (cin >> a >> b)

{

int a1 = stoi(a);

int b1 = stoi(b);

int res = 0; //結果

int c = 0;

string str;

for (a1; a1 <= b1; a1++)

{

res = armstrong(a1, a.length());

if (res == a1)

{

c = c + 1;

str = str + to_string(res) + " ";

}

else if (a1 == b1 && c == 0)

{

cout << "none" <<endl;

c = 0;

}

}

if (c != 0)

{

str.erase(str.length() - 1, 1);

cout << str << endl;

}

}

}

int pow(int a, int b) //計算a的b次方

{

int k = a;

for (int i = 0; i < b; i++)

{

a = a * k;

}

return a;

}

 

int armstrong(int a, int b) //例:1634 = 1^4 + 6^4 + 3^4 + 4^4

{

stringstream temp;

string x = to_string(a);

int res=0;

for (int i = 0; i < b; i++)

{

int n;

temp << x[i];

temp >> n;

res = res + pow(n, x.length());

temp.str("");

temp.clear();

}

return res;

}

 
ZeroJudge Forum