zoukankan      html  css  js  c++  java
  • Poj 2602 Superlong sums(大数相加)

    一、Description

    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 of an input file contains a single number N (1<=N<=1000000) - the length of the integers (in order to make their lengths equal, some leading zeroes can be added). It is followed by these integers written in columns. That is, the next N lines 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.

    Output

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

    二、问题分析

    题目看了几遍,硬是没看懂。后来找到翻译,才知道题意,花了我老鼻子劲了。
    问题:
    一个新的程序语言D++的创造者发现不论如何设计超长整数的范围,有些时候程式设计师还是需要运算更大的数值。1000位的限制太小了,你现在要对两个长度最多有1000000位的整数做加法运算。
    Input
    输入的第一列有一个整数N,代表接下来有几组测试资料。
    每一组测试资料的开头有一整数M(1<=M<=1000000),表示要相加的两个数的长度。为了令两个数字的长度相等,较短的数字开头会补上一连串的0。
    接下来的M列每一列会有两个字元,以空白键作区格,要相加的两个数会以直行的方式表示。拿以下Sample Input为例,要相加的数值分别为4287+0​​463与098+372。
    Output
    对每一组输入印出相加后的结果,每个结果之间请空一行。

    分析:1000000的大数相加,我们的整形是不能承受的。而且此题的数据非常强大,对时间的要求又很严格。由于输入是从高位到地位,因此我们从低位开始计算,并且我们就必须马上算出这个位元的和。又如果发生进位,则把进位值往上一位丢,但是我们不知道进位之后的前一位是否还会再发生进位,因此要一直往前追踪到没有进位发生为止,才能读入下一列的测资。以本题的测资量,若是打算将每一位元的和计算出来后才从最后一位开始往前寻找进位则会超过时间限制。因此仍然不能全部加完之后才处理进位,必须在发生进位时就要马上处理。用Java借此题是要注意用BufferedInputStream将输入读入字节数组。在这里要特别注意索引的值得变化以及'/r' ,'/n' 各占一个字节。读取进来的字节用ASCII码表示,用于计算时要减48(0的Ascii码)。而输出则是输出字节数组,因此要加48转换成ASCII.
    import java.io.BufferedInputStream;
     import java.io.IOException;
    
     public class Main {
    
         public static void main(String[] args) throws NumberFormatException,IOException {
             BufferedInputStream read = new BufferedInputStream(System.in);
             byte[] b = new byte[5000000];
             read.read(b);
             String s = "";
             int index = 0;
             while (b[index] != '
    ') {
                 s += (char) b[index];
                 index++;
             }
             int n = Integer.parseInt(s);
             byte[] c = new byte[n];
             for (int i = 0; i < n; i++) {
                 index++;
                 index++;
                 c[i] += b[index] - 48;
                 index++;
                 index++;
                 c[i] += b[index] - 48;
                 index++;
             }
             int cf = 0;
             for (int i = n - 1; i >= 0; i--) {
                 c[i] += cf;
                 cf=c[i]/10;
                 c[i]=(byte)(c[i] %10 +48);
               
             }
             System.out.write(c);
         } 
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Python之异常篇 [待更新]
    python脚本工具 - 4 获取系统当前时间
    python脚本工具 - 3 目录遍历
    数字签名和数字证书到底是个神马玩意?
    CSRF攻击[转]
    Python之数据结构篇
    Python之模块篇
    Python之函数篇
    python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
    python脚本工具-1 制作爬虫下载网页图片
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734195.html
Copyright © 2011-2022 走看看