package acm1000;
import java.util.Scanner;
public class Main
{
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String s1, s2;
		String SUM = "";
		int t;
		int a, b;
		int num;
		num = sc.nextInt();
		boolean j = false;
		int n = 0;
		do
		{
			s1 = sc.next();
			s2 = sc.next();
			System.out.println("Case " + ++n + ":");
			System.out.print(s1 + " + " + s2 + " = ");
			SUM = "";
			a = s1.length();
			b = s2.length();
			if (a <= b)
			{
				String splus = "";
				for (int p = 0; p < b - a; p++)
					splus += "0";
				s1 = splus + s1;
				a = b;
				t = a;
			}
			else
			{
				String splus = "";
				for (int p = 0; p < a - b; p++)
					splus += "0";
				s2 = splus + s2;
				b = a;
				t = a;
			}
			j = false;
			for (int i = 0; i <= t; i++)
			{
				if ((s1.length() > 0))
				{
					String c;
					c = "" + s1.substring(a - 1, a);
					String d = s2.substring(a - 1, a);
					s1 = s1.substring(0, a - 1);
					s2 = s2.substring(0, a - 1);
					a--;
					int cToInt = Integer.parseInt(c);
					int dToInt = Integer.parseInt(d);
					int sum = cToInt + dToInt;
					if (j == true)
					{
						sum = sum + 1;
					}
					if (sum >= 10)
					{
						j = true;
						sum = sum - 10;
					}
					else if (sum < 10)
						j = false;
					SUM = sum + SUM;
				}
			}
			if (j)
				SUM = 1 + SUM;
			System.out.println(SUM);
			System.out.println();
		}
		while (--num != 0);
	}
}
同JAVA,好像思想上跟你的第二个差不多。  
回复  更多评论