Blue Bridge abbreviated summation JAVA

在电子计算机普及以前,人们经常用一个粗略的方法来验算
四则运算是否正确。
比如:248 * 15 = 3720
把乘数和被乘数分别逐位求和,如果是多位数再逐位求和
,直到是1位数,得
2 + 4 + 8 = 14 ==> 1 + 4 = 5;
1 + 5 = 6
5 * 6
而结果逐位求和为 3
5 * 6 的结果逐位求和与3符合,说明正确的可能性很大!!
(不能排除错误)
**其实,这些内容没什么用,无力吐槽,扰乱思路……**

Please write a computer program, for a given string bitwise sum:
the input is a string of digits, and n represents the number of bits (n <1000);
the output is a number representing the summed bitwise repeatedly result.
Input example:
35379
e.g. Output:
9

可以这么理解: 3+5+3+7+9=27  2+7=9   得出上面的输入输出,这样理解!!

Input another example:
7583676109608471656473500295825
e.g. Output:
1

public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		String n=scanner.next();
		System.out.println(post(n, 0));          //调用方法就可
	}
	public  static int post(String x,int count) {
		for (int i = 0; i < x.length(); i++) {
			count+=x.charAt(i)-'0';              //诸位加起来,String类型转为char
		}
		if (count>10) {
			count=post(String.valueOf(count),0); //和大于2位数时,需要重新赋值,再走一遍循环
		}
		return count;
	}

Small Theater: I saw how those years Mercedes-Benz, to survive the winter, they ushered in the spring.

Published 108 original articles · won praise 113 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43771695/article/details/104570117