要求是这样的:输入一个N位数,范围在1~99 999 999,求这个N位数每一位上的数之和。例如,输入12,输出3;输入234510,输出15.
自己写的码如下(有点乱,各位大神将就看吧):
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,d,e,f;
scanf("%d",&d);
b=0;
for(c=2;c<=8;c++)
{
a=d/((c-1)*10)%10;
b=b+a;
}
e=d%10;
f=b+e;
printf("%d",f);
system("pause");
return 0;
}
执行后结果总是不对。
|