(大数 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 ...
随机推荐
- how are you
#include<stdio.h> int main(){ char sentence[100]; int len=0,j,wordlen=0; gets(sentence ...
- Sprint 冲刺第三阶段第6-10天
这几天一直都在整理我们之前的内容,检查会不会有细节问题.例如界面跳转.颜色等. 因为一直没办法找到guitub存放位置.于是在这里存放一些主代码. MainActivity.java package ...
- Sql语句报ORA-01795: 列表中的最大表达式数为 1000
错误信息:java.sql.SQLException: ORA-01795: 列表中的最大表达式数为 1000,错误信息如下: serviceid是:work -------------other W ...
- Java 一维数组作为参数和返回值
一维数组作为参数: 传数组的引用 创建数组直接传,本质也是传数组的引用 传null public class Test { //数组作为参数时,可以传递3中形式 public void m1(int[ ...
- [转帖]关于网络编程中MTU、TCP、UDP优化配置的一些总结
关于网络编程中MTU.TCP.UDP优化配置的一些总结 https://www.cnblogs.com/maowang1991/archive/2013/04/15/3022955.html 感谢原作 ...
- Linux环境(CentOS)安装维护过程中用到的常见命令
1. yum 安装时需要选择仓库 一般的路径 /etc/repos.d/ 2. 查看安装了哪些软件的 yum list |grep docker installed 的就是已经安装的软件. 3. 卸载 ...
- 关于ListBox在Grid中无法充满的问题
WPF布局的时候发现一个问题,ListBox放在Grid中的时候,老是无法充满整个Grid,但是查看代码也没有设置相关的属性,原来在设置ListBox之前首先要让ListBox的容器充满,然后List ...
- Quartz.Net—MisFire
什么是misfire misfire就是哑火,就是trigger没有得到正常的触发. 1.所有的threadpool都在工作,而且工作时间很长,导致trigger没有threadpool去执行. 2. ...
- obj.attr = 5 == obj._dict_["attr"] = 5,调用super相当于调用父类方法进行重新构造
- Django-website 程序案例系列-5 模态对话框实现提交数据
html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...