ural 1048. Superlong Sums 模拟

1048. Superlong Sums

Time limit: 2.0 second
Memory limit: 16 MB
The creators of a new programming language D++ have found out that whatever limit for SuperLongInt type they make, sometimes programmers need to operate even larger numbers. A limit of 1000 digits is so small… You have to find the sum of two numbers with maximal size of 1 000 000 digits.

Input

The first line contains a single integer  N that is the length of the given integers (1 ≤  N ≤ 1 000 000) . It is followed by these integers written in columns. That is, the next  Nlines contain two digits each, divided by a space. Each of the two given integers is not less than 1, and the length of their sum does not exceed  N. The integers may contain leading zeroes.

Output

Output exactly  N digits in a single line representing the sum of these two integers.

Sample

input output
4
0 4
4 2
6 8
3 7
4750
Problem Author: Stanislav Vasilyev and Alexander Klepinin

Problem Source: Ural State University collegiate programming contest (25.03.2000)

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) {
		new Task().solve();
	}
}

class Task {
	InputReader in = new InputReader(System.in) ;
	PrintWriter out = new PrintWriter(System.out);
	
	String append(int n , int len){
		String s = String.valueOf(n) ;
		int l = s.length() ;
		for(int i = 0 ; i < len - l ; i++){
			s = "0" + s ;
		}
		return s ;
	}

	void solve() {
		int n = in.nextInt() ;
		int base = 8 ;
		int m = n % base == 0 ? n/base : n/base+1 ; 
		int[][] num = new int[m+1][2] ;
		for(int i = m ; i >= 2 ; i--){
			num[i][0] = num[i][1] = 0 ;
			for(int k = 0 ; k < base ; k++){
				num[i][0] = num[i][0] * 10 + in.nextInt() ;
				num[i][1] = num[i][1] * 10 + in.nextInt() ;
			}
		}
		int k = n % base == 0 ? base : n % base ;
		for(int i = 0 ; i < k ; i++){
			num[1][0] = num[1][0] * 10 + in.nextInt() ;
			num[1][1] = num[1][1] * 10 + in.nextInt() ;
		}
		int sum = 0 ;
		sum += num[1][0] + num[1][1] ;
		num[1][0] = sum % (int) Math.pow(10 , k) ;
		sum /= (int) Math.pow(10 , k) ;
		String[] result = new String[m+1] ;
		result[1] = append(num[1][0] , k) ;
		int b = (int) Math.pow(10 , base) ;
		for(int i = 2 ; i <= m ; i++){
			sum += num[i][0] + num[i][1] ;
			num[i][0] = sum % b ;
			sum /= b ;
			result[i] = append(num[i][0] , base)   ;
		}
		for(int i = m ; i >= 1 ; i--){
			out.print(result[i]) ;
		}
		out.println() ;
	    out.flush() ;    
	}
	
}


class InputReader {  
    public BufferedReader reader;  
    public StringTokenizer tokenizer;  
  
    public InputReader(InputStream stream) {  
        reader = new BufferedReader(new InputStreamReader(stream), 32768);  
        tokenizer = new StringTokenizer("");  
    }  
  
    private void eat(String s) {  
        tokenizer = new StringTokenizer(s);  
    }  
  
    public String nextLine() {  
        try {  
            return reader.readLine();  
        } catch (Exception e) {  
            return null;  
        }  
    }  
  
    public boolean hasNext() {  
        while (!tokenizer.hasMoreTokens()) {  
            String s = nextLine();  
            if (s == null)  
                return false;  
            eat(s);  
        }  
        return true;  
    }  
  
    public String next() {  
        hasNext();  
        return tokenizer.nextToken();  
    }  
  
    public int nextInt() {  
        return Integer.parseInt(next());  
    }  
      
    public int[] nextInts(int n){  
        int[] nums = new int[n] ;  
        for(int i = 0 ; i < n ; i++){  
            nums[i] = nextInt() ;  
        }  
        return nums ;  
    }  
  
    public long nextLong() {  
        return Long.parseLong(next());  
    }  
  
    public double nextDouble() {  
        return Double.parseDouble(next());  
    }  
  
    public BigInteger nextBigInteger() {  
        return new BigInteger(next());  
    }  
  
}  
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值