import java.io.IOException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.lang.StringBuffer;
class Main
{
public static final boolean DEBUG = false;
public static void main(String[] args) throws IOException
{
Scanner cin;
int a, b;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(new InputStreamReader(System.in));
}
while (cin.hasNext()) {
a = cin.nextInt();
b = cin.nextInt();
if (a == 0 && b == 0) break;
StringBuffer s1 = new StringBuffer(String.valueOf(a));
StringBuffer s2 = new StringBuffer(String.valueOf(b));
//System.out.println("s1:" + s1);
//System.out.println("s2:" + s2);
s1.reverse();
s2.reverse();
int cnt = 0;
int i;
int len1 = s1.length(), len2 = s2.length();
int carry = 0;
for (i = 0; i < len1 && i < len2; i++) {
int sum = s1.charAt(i) - '0' + s2.charAt(i) - '0' + carry;
//System.out.println(sum);
carry = sum / 10;
cnt += carry;
}
for (; i < len1; i++) {
int sum = s1.charAt(i) - '0' + carry;
carry = sum / 10;
cnt += carry;
}
for (; i < len2; i++) {
int sum = s2.charAt(i) - '0' + carry;
carry = sum / 10;
cnt += carry;
}
if (cnt == 0) {
System.out.println("NO carry operation.");
} else if (cnt == 1) {
System.out.println(cnt + " carry operation.");
} else {
System.out.println(cnt + " carry operations.");
}
}
}
}
题目1143:Primary Arithmetic
最新推荐文章于 2024-01-20 19:49:44 发布