Pages

Monday, August 8, 2011

a040. 阿姆斯壯數


#include
using namespace std;
#include
#include

bool IsArm(int thenum);

int main()
{
int a,b;
while(cin>>a>>b)
{
int flag_hasArm=0;
for(int num=a;num<=b;num++)
{
if(IsArm(num))
{
cout< flag_hasArm=1;
}
}
if(!flag_hasArm)
cout<<"none";
cout< }


return 0;
}


bool IsArm(int thenum)
{

int a[100]={0},sum=0,size=0,temp=thenum;
for(int i=0;temp!=0;i++)
{
a[i] = temp % 10;
temp /= 10;
size++;
}

for(int j=0;j {
sum += pow((float)a[j],size);
}

return ((sum==thenum)? 1:0);
}


/*
bool IsArm(int thenum)
{
char buffer[100];
//string str="0";
itoa(thenum,buffer,10);
string str(buffer);
int sum=0,length=str.length();
for(int i=0;i {
sum += pow( (float)(str[i]-'0'),length);
}

return ((sum==thenum)? 1:0);
}
*/

No comments:

Post a Comment