(大数 string) Integer Inquiry hdu1047
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的更多相关文章
- Integer Inquiry【大数的加法举例】
Integer Inquiry Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27730 Accepted: 10764 ...
- UVa 424 Integer Inquiry 【大数相加】
解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...
- hdu acm-1047 Integer Inquiry(大数相加)
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu1047 Integer Inquiry
/* Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Integer Inquiry 大数加法
Integer Inquiry 1 import java.util.*; 2 import java.math.*; 3 import java.io.*; 4 import java.text.* ...
- HDUOJ-----1074 Integer Inquiry
Integer Inquiry Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 424 - Integer Inquiry
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. He extended his ...
- Integer Inquiry
Integer Inquiry Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Sub ...
- hdu 1047 Integer Inquiry
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Description One of the first use ...
随机推荐
- 《Linux内核设计与实现》课本第四章学习总结
进程调度 4.1 多任务 多任务操作系统就是能同时并发的交互执行多个进程的操作系统. 多任务系统分为两种: 抢占式多任务:Linux提供了抢占式的多任务模式,由调度程序来决定什么时候停止一个进程的运行 ...
- jsp获取传过来的值
request.setCharacterEncoding("utf-8"); String credit=request.getParameter("credit&quo ...
- Github的建立及心得体会
第一次接触Github,这次注册最大的难处就是全英文,着实看不懂.仅凭着认识的几个常用词去了解个具体内容实在是太困难了.所以第一个体会就是要好好学英语背单词,不想看到满屏的英文就感觉头疼,烦躁.第二个 ...
- Git 换行符检查 CRLF 与 LF
遇到的问题 在 git 提交或是签出时,提示如下问题: [git] warning: LF will be replaced by CRLF | fatal: CRLF would be replac ...
- Java关于struts2框架
今天研究了一下struts2框架,我不太喜欢理论的东西,我研究框架更喜欢打断点一步步跟着去看实现的过程.
- html DOM 方法和屬性
html利用javascript對節點執行動作: 每一個節點是一個節點對象,對節點的動作是通過方法和屬性接口實現的: 方法:就是執行的動作: 屬性就是節點的屬性(包括設置和獲取): 常見方法和作用: ...
- jsp servlet 的 请求转发和重定向
以前对于servlet和servlet/jsp之间的跳转路径问题感到很迷惑,今天亲自动手实验了一把,总结如下: servlet已经是项目根路径下面的资源了,所以servlet跳转的时候,在跳转路径上面 ...
- Bootstrap学习目录
前面的话 Bootstrap与CSS的关系,类似于javascript与jQuery的关系,原理与应用的关系.只是jQuery不再火爆,而Bootstrap依然火热,它在github有着超过100万的 ...
- git如何拉取指定分支的代码
问题背景: 新项目还在开发阶段,没有正式对外发布,所以开发同事合并代码到develop上(或者其他名称分支上),而不是到master分支上 通过git拉取代码的时候,默认拉取的是master分支,如下 ...
- BZOJ3523[Poi2014]Bricks——贪心+堆
题目描述 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合法的方案或判断 ...