Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 22451    Accepted Submission(s): 6099

Problem 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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input

1

123456789012345678901234567890

123456789012345678901234567890

123456789012345678901234567890

0

Sample Output

370370367037037036703703703670

可以用string的高精度模板进行加法运算,
#include<iostream>
#include<string>
using namespace std;
string add(string str1,string str2)
{
int len1=str1.length();
int len2=str2.length();
if(len1>len2) //为了让长度较小的字符串前面补0,使两个字符串的长度相等。
{
for(int i=;i<=len1-len2;i++)
str2=""+str2;
}
else
{
for(int i=;i<=len2-len1;i++)
str1=""+str1;
}
len1=str1.length();
int cf=;
int t;
string str;
for(int i=len1-;i>=;i--) //因为最后一位数的下标就是len1-1;首位的为0。
{
t=str1[i]-''+str2[i]-''+cf;
cf=t/; //进位。
t%=;
str=char(t+'')+str;
}
if(cf!=) str=char(cf+'')+str;
return str;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string sum="";
string str1;
while(cin>>str1) //可以用这方法进行循环输入字符串。
{
if(str1=="")break;
sum=add(sum,str1);
}
cout<<sum<<endl;
if(t)
cout<<endl;
}
return ;
}
用java:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int t=in.nextInt();
while(t>0) {
t--;
BigInteger a=new BigInteger("0");
while(in.hasNextBigInteger()) {
BigInteger b=in.nextBigInteger();
if(!b.equals(BigInteger.valueOf(0))) {
a=a.add(b);
}
else {
System.out.println(a);
if(t!=0)
System.out.println();
break;
}
}
}
}
}

(大数 string) Integer Inquiry hdu1047的更多相关文章

  1. Integer Inquiry【大数的加法举例】

    Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27730   Accepted: 10764 ...

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

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

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

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

  4. hdu1047 Integer Inquiry

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

  5. Integer Inquiry 大数加法

    Integer Inquiry 1 import java.util.*; 2 import java.math.*; 3 import java.io.*; 4 import java.text.* ...

  6. HDUOJ-----1074 Integer Inquiry

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

  7. 424 - Integer Inquiry

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

  8. Integer Inquiry

    Integer Inquiry Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Sub ...

  9. hdu 1047 Integer Inquiry

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...

随机推荐

  1. 对MSF八个原则的思考

    第一个原则,也是MSF中最基础的一个原则,推动信息共享与沟通.这个原则的一个特点是,对于团队成员的所有工作,都会被记录下来,包括走了弯路的.出现bug但已调试解决的部分.对于新加入团队的成员或者以前没 ...

  2. 个人阅读作业 --软件工程M1/M2总结

    软件工程M1/M2总结 写在前面的话: 这学期的软件工程伴着考期的展开逐渐落下帷幕,回顾这学期的软件工程,我感觉我的热情在一次又一次的失落中逐步消耗殆尽,每个人对于这门课的体验都会有所不同吧,可以确定 ...

  3. 个人阅读作业WEEK7 (软件工程的瀑布, 大泥球, 教堂,集市,和银弹)

    一 . 关于银弹 (Silver Bullet) 银弹,被引申为解决问题的有效办法.IBM大型机之父福瑞德·布鲁克斯在1986年的论文<没有银弹>中表达了他的观点:软件工程中不存在银弹—— ...

  4. github心得体会 王倩倩 201303014004 计科高职13-1

    刚开始接触一门语言软件特别无从下手,尤其还是全英文的,真的是很头疼,注册的时候这个就弄了半天,在网上搜了一下教程然后又结合着老师上课讲的内容自己多做了几遍,也算是对github熟悉了,然后学会操作代码 ...

  5. Github的建立及心得体会

    第一次接触Github,这次注册最大的难处就是全英文,着实看不懂.仅凭着认识的几个常用词去了解个具体内容实在是太困难了.所以第一个体会就是要好好学英语背单词,不想看到满屏的英文就感觉头疼,烦躁.第二个 ...

  6. HDOJ2004_成绩转换

    水题:用数组标识各个阶段分数的等级即可. HDOJ2004_成绩转换 #include<stdio.h> #include<stdlib.h> #include<math ...

  7. Java实现小学四则运算练习系统(UI)

    github项目地址 :https://github.com/feser-xuan/Arithmetic_test3_UI 小伙伴的博客链接:http://www.cnblogs.com/fukang ...

  8. [2017BUAA软工]个人项目

    软工个人项目 一.Github项目地址 https://github.com/Lydia-yang/2017BUAA-SoftwareEngineering 二.解题思路 在刚开始拿到题目的时候,关于 ...

  9. ThreadPoolExecutor源码解读

    1. 背景与简介 在Java中异步任务的处理,我们通常会使用Executor框架,而ThreadPoolExecutor是JUC为我们提供的线程池实现. 线程池的优点在于规避线程的频繁创建,对线程资源 ...

  10. PAT 1003 我要通过!

    https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192 “答案正确”是自动判题系统给出的最令人欢喜的 ...