Disarium Number (Special Numbers Series #3)
DefinitionDisarium 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;
char ss;
int i = 0, j = 0;
if (n < 0)
{
m = 0 - m;
j = 1;
ss = '-';
}
while (m > 0)
{
s = m % 10 + '0';
m /= 10;
}
s = '\0';
i = i - 1;
while (i >= 0)
{
ss = s;
}
ss = '\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);
}
if (n == number) return "Disarium !!";
return "Not !!";
}
页:
[1]