马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Definition
Disarium number is the number that The sum of its digits powered with their respective positions is equal to the number itself.
Task
Given a number, Find if it is Disarium or not .
#include <string>
#include <cmath>
using namespace std;
string int2str(int n)
{
int m = n;
char s[100];
char ss[100];
int i = 0, j = 0;
if (n < 0)
{
m = 0 - m;
j = 1;
ss[0] = '-';
}
while (m > 0)
{
s[i++] = m % 10 + '0';
m /= 10;
}
s[i] = '\0';
i = i - 1;
while (i >= 0)
{
ss[j++] = s[i--];
}
ss[j] = '\0';
string res = ss;
return res;
}
int char2int(char ch)
{
switch (ch)
{
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
}
}
string disariumNumber (int number )
{
string res = int2str(number);
int n = 0;
for (int i = 1; i <= res.size(); i++) {
n += pow(char2int(res[i - 1]), i);
}
if (n == number) return "Disarium !!";
return "Not !!";
}
|