zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Vuex的使用
    vue的props属性,vue的插槽
    ES6 Promise对象
    ES6 Map对象以及Set对象
    函数作用域以及块级作用域
    组件之间的传值-$refs&$parent
    Vue中父子组件的传值
    v-on 以及v-model的修饰符以及vue的常用指令
    时间线
    readline和xreadline的区别
  • 原文地址:https://www.cnblogs.com/cznczai/p/11148074.html
Copyright © 2011-2022 走看看