🌕🌑🌑🌑🌑

# 題目連結

  • 題目連結
  • Online Judge
  • uDebug

# 題目說明

Time limit: 3.000 seconds

# 題目

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.

# 解題技巧

# Solution

Main.java
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String num1 = sc.next();
        String num2 = sc.next();
        while (num1.charAt(0) != '0' || num2.charAt(0) != '0') {
            int carry = 0;
            int count = 0;
            int max = Math.max(num1.length(), num2.length());
            while (num1.length() < max) {
                num1 = "0" + num1;
            }
            while (num2.length() < max) {
                num2 = "0" + num2;
            }
            for (int i = max - 1; i >= 0; i--) {
                int n1 = (int) (num1.charAt(i) - '0');
                int n2 = (int) (num2.charAt(i) - '0');
                int sum = n1 + n2 + carry;
                carry = sum / 10;
                if (carry == 1) {
                    count++;
                }
            }
            if (count == 0) {
                System.out.println("No carry operation.");
            } else {
                System.out.println(count + " carry " + (count > 1 ? "operations." : "operation."));
            }
            num1 = sc.next();
            num2 = sc.next();
        }
        sc.close();
    }
}
單字

** **
!! !!

片語 & 搭配詞

!! !!