题目链接
数的价值(一)
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss
Problem Description
一个数的价值定义如下:一个数的价值等于这个数的各数位之和。
现在ldq很多正整数,可是他现在忙于各种acm比赛,没空计算。
现在请你来写个程序帮他来计算每个数的价值是多少。
Input
输入包含多组测试数据。
首先输入一个整数T(1<=T<=100)
接下来有T行
每组测试数据只包含一个正整数 n (1 < n <= 10^6)。
Output
对于每组测试数据,输出一个数m代表n的价值
Example Input
1
701
Example Output
8
Hint
Author
axuhongbo
思路:很简单的水题,求个位数之和,有两种做法,一种是直接while循环,当数据量大时也可以采用用字符串读入的方法去做。
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int s;
scanf("%d",&s);
int sum = 0;
while(s)
{
sum+=s%10;
s/=10;
}
printf("%d\n",sum);
}
return 0;
}