关于pow函数的运用——判断水仙花数
大家应该知道水仙花数吧!
然而利用pow函数为什么会出现这样的错误呢?
#include <iostream>
#include <cmath>
using namespace std;
int weishu(int m)
{
int k=0;
while(m!=0)
{
k+=1;
m/=10;
}
return k;
}
int main()
{
int m, s=0,k,a;
while(cin>>m){
s=0; //加,否则从第2个数开始出错
a=m;
k=weishu(m);
cout << k <<endl;
while(m!=0)
{
s+= (int)pow((float)(m%10),k);//改 s+=pow(m%10,k);
m/=10;
}
cout << a <<' '<< s <<endl;//改 count << a <<' '<< s <<endl;
if(a==s)
cout<<"水仙花数"<<endl;
else
cout<<"不是水仙花数"<<endl;
}
return 0;
}