例如:1729——1+7+2+9=19
int DigitSum(unsigned int num)
{
if (num > 9)
{
return DigitSum(num / 10) + num % 10; //利用递归把右边那位拿出来相加
}
else
{
return num;
}
}
int main()
{
unsigned int num = 0;
scanf("%d", &num);
int ret = DigitSum(num);
printf("ret=%d\n", ret);
return 0;
}