Fork me on GitHub

1002.A + B Problem II --大数问题


不得不说这道题坑!!!! 格式化输出 真的很D区 不格式化就错 wuwuwuwuwuuwu

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int i = 1;
		while (n > 0) {
			// 注意用next 不可以用nextLine() 才能输入两个数值
			String a = sc.next();
			String b = sc.next();
			// 因为数不超过1000位,用一个数组存放大数
			int c[] = new int[1001];
			// 输入a 得到的长度
			int lengthA = a.length();
			// 输入b 得到的长度
			int lengthB = b.length();
			// 得到数组的长度
			int lengthC = c.length;
			// 一开始进位为0
			int temp = 0;
			int flag = Math.min(lengthA, lengthB);
			while (flag > 0) {
//看看两个数的末尾
				int i1 = a.charAt(lengthA - 1) - '0';
				int i2 = b.charAt(lengthB - 1) - '0';
				c[lengthC - 1] = (i1 + i2 + temp) % 10;
				temp = (i1 + i2 + temp) / 10;
				lengthA--;
				lengthB--;
				lengthC--;
				flag--;
			}
//看那个数还没完全处理完 继续处理
			flag = (lengthA > lengthB) ? lengthA : lengthB;
			String tx = (lengthA > lengthB) ? a : b;
			if (flag > 0) {
				while (flag > 0) {
					c[lengthC - 1] = (temp + tx.charAt(flag - 1) - '0') % 10;
					temp = (temp + tx.charAt(flag - 1) - '0') / 10;
					flag--;
					lengthC--;
				}
			}
//逐个加到字符串里面
			c[lengthC - 1] = temp;
			StringBuffer str = new StringBuffer("");
			boolean x = false;
			for (int i2 = 0; i2 < c.length; i2++) {
				if (c[i2] != 0) {
					x = true;
				}
				if (x == true) {
					str = str.append(c[i2]);
				}
			}
			System.out.println("Case " + i + ":");
			System.out.print(a + " + " + b + " = " + str);
//			敲重点 在这里有问题 格式
            if(n != 1){
                 System.out.println();
             }
             System.out.println();
			n--;
			i++;
		}
	}
}
#include "pch.h"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#include <iomanip>
#define maxn 15
#define MAXN 100005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define exp 1e-6
#define pi acos(-1.0)
using namespace std;
int main()
{
	//freopen("D:\\a.txt","r",stdin);
	ios::sync_with_stdio(false);
	char a1[1000], b1[1000];
	int  a[1000], b[1000], c[1000], lena, lenb, lenc, i, x;
	int t, cas = 0;
	cin >> t;
	while (t--)
	{
		cas++;
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(c, 0, sizeof(c));
		cin >> a1 >> b1;
		lena = strlen(a1);
		lenb = strlen(b1);
		for (i = 0; i <= lena - 1; i++)
			a[lena - i] = a1[i] - 48;
		for (i = 0; i <= lenb - 1; i++)
			b[lenb - i] = b1[i] - 48;
		lenc = 1;
		x = 0;
		while (lenc <= lena || lenc <= lenb)
		{
			c[lenc] = a[lenc] + b[lenc] + x;
			x = c[lenc] / 10;
			c[lenc] %= 10;
			lenc++;
		}
		c[lenc] = x;
		if (c[lenc] == 0)
			lenc--;
		cout << "Case " << cas << ":" << endl;
		for (i = 0; i <= lena - 1; i++)
			cout << a1[i];
		cout << " + ";
		for (i = 0; i <= lenb - 1; i++)
			cout << b1[i];
		cout << " = ";
		for (i = lenc; i >= 1; i--)
			cout << c[i];
		cout << endl;
		if (t)
			cout << endl;
	}
	return  0;
}
posted @ 2019-07-07 21:54  cznczai  阅读(319)  评论(0编辑  收藏  举报