一、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] != '\r') {
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);
}
}

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

Poj 2602 Superlong sums(大数相加)的更多相关文章

  1. POJ 1503 Integer Inquiry(大数相加)

    一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...

  2. Poj OpenJudge 百练 2602 Superlong sums

    1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlo ...

  3. POJ 1503 Integer Inquiry(大数相加,java)

    题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...

  4. hdu acm-1047 Integer Inquiry(大数相加)

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. UVa 424 Integer Inquiry 【大数相加】

    解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...

  6. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  7. 随机数组&大数相加

    随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中 一,      设计思路: 先生成随机数数组,再将数组保存在一个字符串中,然后将数组各数字加和, ...

  8. java-两个大数相加

    题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...

  9. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

随机推荐

  1. nginx中使用waf防火墙

    1.安装依赖 yum install -y readline-devel ncurses-devel 2.安装Lua # .tar.gz # cd lua- # make linux # make i ...

  2. [Delphi]解决Delphi Distiller运行报错"HKEY_CURRENT_USER\\" is of wrong kind or size

    最近终于决心将使用多年的Delphi 7升级到Delphi 2007,虽然目前Delphi最高版本已经是XE8,但对于只做VCL开发的话还是喜欢2007这个经典的版本. 安装Delphi 2007一切 ...

  3. [Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] The child node "db_driver" at path "fos_user" must be configured.

    $ php bin/console server:run [Symfony\Component\Config\Definition\Exception\InvalidConfigurationExce ...

  4. 去掉标题栏/ActionBar后点击menu键时应用崩溃

    MainActivity 继承了 ActionBarActivity后,想要去掉标题栏(ActionBar),在程序中加上requestWindowFeature(Window.FEATURE_NO_ ...

  5. 求阶乘,输入一个正整数 n,输出n!

    #include<stdio.h>int factorial (int n); int main(){ int n; scanf("%d",&n); print ...

  6. PAT 天梯赛 L2-005. 集合相似度 【SET】

    题目链接 https://www.patest.cn/contests/gplt/L2-005 思路 因为集合中的元素 是不重复的 所以用SET 来保存 集合 然后最后 查找一下 两个集合中共有元素 ...

  7. 【leetcode刷题笔记】Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  8. OJ 之 FATE

                                            - FATE Crawling in process... Crawling failed Time Limit:100 ...

  9. Luogu-1381 单词背诵

    先将n个单词插入哈希表,记录左右端点,每次右端点往后移动,读入一个新的单词并记录下它的哈希值,如果这个单词之前没出现过那么更新\(ans\)和\(minl\),如果左端点的单词出现了不止一次则可以往右 ...

  10. perl常用字符串函数

    1.$position = index(string,substring,skipchars): 该函数返回子串substring在字符串string中的位置,如果不存在,则返回-1:参数skipch ...