(大数 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 ...
随机推荐
- c++入门之关于cin,cout以及数据流的认识
- Individual P1: Summary
经过5个小时成功把simple mode写差不多了..orz 也是蛮拼的. 开始毫无头绪,本能地开始从度娘搜索‘c# 单词统计’= =看了两段代码也算是见过c#的人了.差不多花了我1小时的时间. 然后 ...
- ☆C++学习心得
C++是我进大学的学的第一种编程语言,在高中的时候有电脑课,有教过部分的VB语言,所以其实对编程也并不是非常的陌生,刚开是上课也觉得感觉不难,都懂,没多少课后,恍了个神..居然听不懂了!老师经常让我们 ...
- Tools (StExBar vs Cmder)which can switch to command line window on context menu in windows OS
https://tools.stefankueng.com/StExBar.html https://github.com/cmderdev/cmder
- Linux:cut命令详解
cut 文件内容查看 显示行中的指定部分,删除文件中指定字段 显示文件的内容,类似于下的type命令. 说明 该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的 ...
- sap 最新财报以及云业务转型情况
SAP第四季度收入超预期 加码云转型启动重组计划 http://soft.zhiding.cn/software_zone/2019/0130/3115457.shtml 尽管第四季度超出收入预期,但 ...
- Win10 1803 升级之后无法使用 共享目录的解决方法
1. 自己的虚拟机升级了win10 最新版本 1803 但是发现 使用共享文件夹时报错: 2, 百度发现解决方案还是非常简单的. 需要安装一个 功能 3.方法: 开始 运行 输入 control 打开 ...
- [学习笔记]Ubuntu下安装配置SQLSERVER2017
摘要自微软官网: https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-ubuntu 安装步骤: 1. 导入公共秘 ...
- DispatcherServlet源码分析
一.客户端发送请求的总体过程 DispatcherServlet是SpringMVC的入口,DispatcherServlet其实也是一个Servlet.服务器处理客户端请求的步骤如下: 1.客户端发 ...
- mysql数据库优化大全
转载:https://blog.csdn.net/weixin_38112233/article/details/79054661 数据库优化 sql语句优化 索引优化 加缓存 读写分离 分区 分布式 ...