一、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. 3.设计模式----TemplateMethod模式

    模板模式,其实是一种思想,在开发中有很多地方用到模板,因为毕竟我们不可能每一个都一出一段!一个模板,填充不同,出来效果也是不一样! 准备画个时序图的,没找到工具,过几天补上! 模板模式在出现bug时候 ...

  2. Frobenius Norm

    http://mathworld.wolfram.com/FrobeniusNorm.html

  3. linux c编程:管道

    2在前面介绍过,进程之间交换信息的唯一途径就是传送打开的文件.可以经由fork或者exec来传送.这一章将介绍新的进程共享方式 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都 ...

  4. IE下获取不到Response添加的cookie的解决方法

    原博客地址: http://blog.csdn.net/wjdd1/article/details/16341189 在百度上查询了好久也没有查询到结果,于是自己用ie的开发者工具进行跟踪,JSESS ...

  5. Intel Quick Sync Video Encoder 2

    这边博客主要记录在预研quick sync中涉及到的一些性能质量相关的关键参数设置. github: https://github.com/MarkRepo/qsve 1. VPP处理过程伪代码: M ...

  6. 从分布式锁来看redis和zookpeer!

    从分布式锁来看redis和zookpeer! 目前网上大部分的基于zookpeer,和redis的分布式锁的文章都不够全面.要么就是特意避开集群的情况,要么就是考虑不全,读者看着还是一脸迷茫.坦白说, ...

  7. hive查询注意及优化tips

    Hive是将符合SQL语法的字符串解析生成可以在Hadoop上执行的MapReduce的工具.使用Hive尽量按照分布式计算的一些特点来设计sql,和传统关系型数据库有区别, 所以需要去掉原有关系型数 ...

  8. docker swarm部署spring cloud服务

    一.准备docker swarm的集群环境 ip 是否主节点   192.168.91.13 是   192.168.91.43 否   二.准备微服务 ①eureka服务 application.y ...

  9. HDU - 1003 Max Sum 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意 给出一个序列 要求找出一个和最大的子序列 思路 O(N)的做法 但是要标记 子序列的头部位 ...

  10. 事件监听机制——列出指定目录内容、添加Dialog对话框

    事件监听机制理解与Dialog练习 利用Java语言,仿照我的电脑目录进行打开目录,输入文件路径,查看该路径下所有的文件,设置两个文本框,一个转到按钮,当点击转到按钮时,查看路径是否正确,若正确在第二 ...