題目連結: https://zerojudge.tw/ShowProblem?problemid=c459
# 解題思路
這題就依照題目的敘述,先算出位數,順便將數字轉為 10 進位,之後再依序題意,計算出偽自戀數再判斷吧
(當年的我還真是厲害,竟然會想用 while 去做)
# 程式碼
#include <bits/stdc++.h> | |
using namespace std; | |
int main(){ | |
int b,N,N1,N2,n,n1,s,s1,s2; int i; | |
cin>>b>>N; | |
N1=N; N2=N; s=0; s1=0; s2=0; | |
// 轉 10 進位 | |
while(N1/10!=0) { | |
s++,n=N1%10; | |
N1/=10; | |
for(i=2;i<=s;i++)n*=b; | |
s1+=n; | |
if(N1/10==0){ //N<10 | |
s++,n=N1; | |
for (i=2;i<=s;i++)n*=b; | |
s1+=n; | |
} | |
} | |
// 計算自戀數 | |
while (N2/10!=0) { | |
n=N2%10; n1=N2%10; | |
N2/=10; | |
for (i=2;i<=s;i++)n1*=n; | |
s2+=n1; | |
if (N2/10==0)//N<10 | |
{ | |
n=N2; n1=N2; | |
for (i=2;i<=s;i++) n1*=n; | |
s2+=n1; | |
} | |
} | |
cout<<(s1==s2?"YES":"NO")<<"\n"; | |
} |