标签:
试题编号: 201512-1
试题名称: 数位之和
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
1 package ccf_test2015_12; 2 3 import java.util.Scanner; 4 5 public class PositionSum { 6 7 public static void main(String[] args) { 8 9 Scanner in = new Scanner(System.in); 10 11 while (in.hasNext()) { 12 13 int num = in.nextInt(); 14 15 int sum = getSum(num); 16 17 System.out.println(sum); 18 19 } 20 } 21 public static int getSum(int num) { 22 23 int sum = 0; 24 25 while (num > 0) { 26 27 int temp = num % 10; 28 29 sum += temp; 30 31 num = num / 10; 32 33 } 34 35 return sum; 36 37 } 38 }
运行结果:
标签:
原文地址:http://www.cnblogs.com/haimishasha/p/5352038.html