一、Description

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.


``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).




The final input line will contain a single zero on a line by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

二、题解

        这个题和之前2602有Poj 2602 Superlong sums(大数相加)异曲同工。但还是有要注意的部分,就是这里有前导的0,而且题目没说是否每个数的位数是否相同。还有数组的长度也比题目说明的要大,所以开数组的时候千万不要手下留情啊。这个题目我WA了还多次,可能是考虑的情况不够全面,对题目的要求没有完全弄明白。所以,虽然经过了几次的调试,几组测试数据都过了,但是还是没有AC。请大神指教。但当我一头雾水的时候呢,居然发现java有一种脑残解法,由于java 中自带的 BigInteger 类,这是个不可变的任意精度的整数。而且接受十进制的字符串,并能将 BigInteger
的十进制字符串表示形式转换为 BigInteger。所以,管它什么大数,都弱爆了。但是看来好东西虽然存在,但是了解它的原理还是必要的。

三、java代码

import java.util.*;
import java.math.*; public class Main { public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigDecimal bd1 = BigDecimal.valueOf(0);
BigDecimal bd2 = BigDecimal.valueOf(0);
String str; while(cin.hasNext()){
str = cin.nextLine();
if(str.equals("0"))
break;
else{
bd2 = new BigDecimal(str);
bd1 = bd1.add(bd2);
}
} System.out.println(bd1.toPlainString());
}
}

求错误代码!!

import java.io.IOException;
import java.util.Scanner; public class Main {
static byte[] c = new byte[1000];
public static void add(String s1){
int i;
for (i = 0; i <s1.length(); i++){
c[i] += s1.charAt(i) - 48;
}
int cf = 0;
for (i = s1.length() - 1; i >= 0; i--) {
c[i] += cf;
cf=c[i]/10;
c[i]=(byte) (c[i] %10);
}
}
public static void main(String[] args) throws IOException {
Scanner cin = new Scanner(System.in);
String s[]=new String[120];
int i=0,j=0,k;
String ss,te;
String te2;
int max=0;
while(!(ss=cin.next()).equals("0")){
s[i]=ss;
max=Math.max(s[i].length(), max);
i++;
}
for(k=0;k<i;k++){
te=s[k];
te2 ="";
while(s[k].length()<max){
s[k]+="0";
te2+="0";
}
s[k]=te2+te;
add(s[k]);
}
while(c[j]==0){
j++;
}
for(k=j;k<max;k++){
System.out.print(c[k]);
}
System.out.println();
}
}

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

POJ 1503 Integer Inquiry(大数相加)的更多相关文章

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

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

  2. POJ 1503 Integer Inquiry 大数 难度:0

    题目链接:http://poj.org/problem?id=1503 import java.io.*; import java.math.BigInteger; import java.util. ...

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

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

  4. Poj 1503 Integer Inquiry

    1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS   Memory Limit: 1000 ...

  5. HDU 1047 Integer Inquiry 大数相加 string解法

    本题就是大数相加,题目都不用看了. 只是注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio ...

  6. POJ 1503 Integer Inquiry 简单大数相加

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

  7. poj 1503 Integer Inquiry (高精度运算)

    题目链接:http://poj.org/problem?id=1503 思路分析: 基本的高精度问题,使用字符数组存储然后处理即可. 代码如下: #include <iostream> # ...

  8. Integer Inquiry(大数相加)

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

  9. Poj 2602 Superlong sums(大数相加)

    一.Description The creators of a new programming language D++ have found out that whatever limit for ...

随机推荐

  1. Linux学习路线指南

    转载的,感觉写的挺好的,我自己知识复制了下,忘记了转载地址,抱歉! Linux学习路线指南 很多同学接触Linux不多,对Linux平台的开发更是一无所知.而现在的趋势越来越表明,作为一个优秀的软件开 ...

  2. 页游手游服务器(三)lua网络层

    在(一)中,介绍了c对lua层的拓展,这边主要写lua层的网络部分,目标是实现RPC调用,实现主要是三部分: 1通信协议(消息的序列化和反序列化) rpc调用主要信息包括方法名,方法参数,设计的通信协 ...

  3. restful规范和restframework框架

    什么是接口? 接口可以理解为url就是接口. 那么在其他语言里面接口也可以是约束类 restful规范是什么? RESTful是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便 ...

  4. java基础入门之九九乘法表

    /* 自学java 九九乘法表 Power by Stuart Date: 2015.4.23 */public class Math { public static void main (Strin ...

  5. JVM性能优化, Part 2 ―― 编译器

    作为JVM性能优化系列文章的第2篇,本文将着重介绍Java编译器,此外还将对JIT编译器常用的一些优化措施进行讨论(参见“JVM性能优化,Part 1″中对JVM的介绍).Eva Andreasson ...

  6. linux awk数组相关操作介绍

    用awk进行文本处理,少不了就是它的数组处理.那么awk数组有那些特点,一般常见运算又会怎么样呢.我们先看下以下的一些介绍,结合样例我们会解说下它的不同之处.在 awk 中数组叫做关联数组(assoc ...

  7. vim中设置tab的长度的方法

    linux下使用vim编程是比較常见的事情,但vim默认的tab是8个空格.但一般的编辑器是4个空格,所以希望改动下.详细方法例如以下:1. 创建文件名称为 .vimrc 的系统文件首先切换到用户根文 ...

  8. value too great for base (error token is "08")

    shell 中,经常有定时任务, 这时候shell脚本中一般会对时间进行一些判断,或者相关逻辑的操作 这时候,如果你获取的小时或者分钟是08,09,如果要再对其进行运算符或者比较的话,就会报标题的错误 ...

  9. 教你如何写一个 Yii2 扩展

    前言 把一系列相关联的功能使用模块开发,好处多多,维护起来很方便,模块还可以单独发布出去,让下一个项目之间使用,真是方便. 下面我就写一个开发扩展的简单教程. Gii gii 自带帮助我们生成一个基本 ...

  10. VHDL学习笔记——数字系统设计

    数字系统是指有若干数字电路和逻辑不见构成的能够处理或传输数字信息的设备.数字系统可分为三部分:输入输出接口.数据处理器和控制器. [传统的系统硬件设计]方法是(1)采用自底向上的设计方法(2)采用通用 ...