1. 程式人生 > >day2--Primary Arithmetic(小學生算術)

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不過的原音,請一定一定一定要告訴我!!!!ありがとうございます。。