CCF CSP Brush Question Record 10-The sum of digits of 201512-1 (java)

Question number: 201512-1
Question name: Sum of digits
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Given a decimal integer n , output the sum of the digits of n .

Input format

  Enter an integer n .

Output format

  Output an integer to indicate the answer.

Sample input

20151220

Sample output

13

Sample description

  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.

 

import java.util.Scanner; 
public class 数位之和 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		
		String[] s=(sc.nextLine()+"").split("");
		int res=0;
		for(int i=0;i<s.length;i++){
			res+=Integer.parseInt(s[i]);
		}
		System.out.println(res);
	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108291608