day2--Primary Arithmetic(小学生算术)

Primary Arithmetic(小学生算术)


看到这道题,第一眼,窃喜。然并。。。。。被坑了一天了。。。
我也不知道怎么回事。。。在洛谷OJ总是不过,一气之下去ACM上搜了一下,刚好有这题,复制提交。。。结果如下图,真的是气死我了。。让我对洛谷产生了深深的怀疑
在这里插入图片描述


切入正题

题目描述

Children are taught to add multi-digit numbers from right-to-left one digit at a time. Many find the “carry” operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge. Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.

Input

Each line of input contains two unsigned integers less than 10 digits. The last line of input contains ‘0 0’.

Output

For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers, in the format shown below.

Sample Input

123 456
555 555
123 594
0 0

Sample Output

No carry operation.
3 carry operations.
1 carry operation.

大写的注意

请睁大眼睛观察输出的单复数哟。

java 代码实现

import java.util.Scanner;

public class Primary_Arithmetic {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner sc = new Scanner(System.in);		
		while(sc.hasNextLine()){
			String str = sc.nextLine();
			String[] num = str.split(" ");
			int[] str_int = new int[num.length];
			int a=0,b=0;
			for(int i=0; i<str_int.length;i++){
				str_int[i]= Integer.parseInt(num[i]);
				a = str_int[0];
				b = str_int[1];
			}
			if(a==0 && b==0){
				break;
			}
			int carry = 0, count = 0;
			while(a != 0 || b != 0){
				carry = (a % 10 + b % 10 +carry) > 9 ? 1 : 0;
				count += carry;
				a /= 10;
				b /= 10;
			}
			if(count ==0){
				System.out.println("No carry operation.");
			}
			else if(count == 1){
				System.out.println("1 carry operation.");
			}
			else{
				System.out.println("" + count + " carry operations.");
			}
		}
	}

}

再见了朋友,要是发现我为什么在OJ不过的原因,请一定一定一定要告诉我!!!!ありがとうございます。。

猜你喜欢

转载自blog.csdn.net/sinat_41879565/article/details/82791120