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. 软件工程(四)数据流图DFD

    结构化分析中,常用到数据模型为实体关系图,功能模型是数据流图 DFD 可以认为,一个基于计算机的信息处理系统由数据流和一系列的转换构成,这些转换将输入数据流变换为输出数据流.数据流图就是用来刻画数据流 ...

  2. 必应语音API(Bing text to speech API)

    前言 Link : Microsoft Speech API overview 通过这个链接,大致了解Bing speech API的语音识别和语音合成两部分, 这次是需要用到TTS,所以就直接看TT ...

  3. jQuery中click事件多次触发解决方案

    jQuery 中元素的click事件中绑定其他元素的click事件. 因为jQuery中的click事件会累计绑定,导致事件注册越来越多. 解决方案: 1.能够避开,避免把click事件绑定到其他元素 ...

  4. MSTSC 3389 端口修改

    1. 启动注册表编辑器. 2. 找到并单击以下注册表子项: 3. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server ...

  5. [CNBETA]动图告诉你 光速到底有多慢?

    https://www.cnbeta.com/articles/tech/811381.htm 我们知道,30万公里每秒的光速是宇宙内目前已知的最高速度,至少现有人类理论体系下它是不可跨越的.30万公 ...

  6. Lodop打印条码二维码的一些设置

    Lodop绘制条码图功能让条码打印变得很简单,客户端不用安装专门的条码字库,该函数格式如下:ADD_PRINT_BARCODE(Top,Left,Width,Height,BarCodeType,Ba ...

  7. 51nod1268(基础dfs)

    解题思路:直接搜索找就行了,搜两边,一个是加入这个数字,一边是不加入这个数字 代码: #include<iostream>#include<algorithm>#define ...

  8. mybatis 缓存(cache)的使用

    许多应用程序,为了提高性能而增加缓存, 特别是从数据库中获取的数据. 在默认情况下,mybatis 的一级缓存是默认开启的.类似于hibernate, 所谓一级缓存,也就是基于同一个sqlsessio ...

  9. ELK--filebeat nginx模块

    Nginx模块 该nginx模块解析由Nginx HTTP服务器创建的访问和错误日​​志 . 当你运行这个模块的时候,它会执行一些任务: 设置日志文件的默认路径(但不用担心,可以覆盖默认值) 确保每个 ...

  10. ajax 调用 java webapi 多个参数(二)

    第一种方法:http://blog.csdn.net/hanjun0612/article/details/74436273 附上另一种解决方法. 这个方法主要针对  嵌套模型(模型中含有模型)的ap ...