【CCF】——Sum of Digits

Problem description
  Given a decimal integer n, output the sum of the digits of n.
Input format
  Input an integer n.
Output format
  outputs an integer to indicate the answer.
Sample input

20151220

Sample output

13

The example shows that the
  sum of the digits of 20151220 is 2+0+1+5+1+2+2+0=13.
Evaluation use case scale and conventions
  All evaluation use cases meet: 0 ≤ n ≤ 1000000000.

By evaluating the scale and regulations of the use case, it can be seen that n does not exceed the range of integer, just store it directly in integer
#include <iostream>
using namespace std;

int main(){
    
    
	ios::sync_with_stdio(false);
	int n,sum = 0;
	cin >> n;
	while(n!=0){
    
    
		sum += n%10;
		n /= 10;
	}
	cout << sum;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/108548840