全部博文(68)
分类: C/C++
2012-01-18 20:46:54
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?
答案:1366
#include
#include
int main(int argc, char *argv[])
{
int n = 1000;
char r[512]={0};
int sum = 0;
int i=0, t=0, j=0;
int flag=0;
int len=1;
r[0] = 2;
for(i=2; i<=n; i++)
{
for (j=0; j
if (1 == flag)
t = 2 * (int)(r[j]) + 1;
else
t = 2 * (int)(r[j]) ;
if (t<10){
r[j] = t;
flag = 0;
}
else{
r[j] = t%10;
flag = 1;
}
}
if (1 == flag)
t = 2 * (int)(r[len-1]) + 1;
else
t = 2 * (int)(r[len-1]) ;
if (t<10){
r[len-1] = t;
flag = 0;
}
else{
r[len-1] = t%10;
flag = 0;
r[len] = 1;
len++;
}
}
for (i=0; i
printf("The sum is %d\n", sum);
return 0;
}