POJ 1503 Integer Inquiry 简单大数相加
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.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
string doo(string str1,string str2){
string s;
int str11=str1.length();
int str22=str2.length();
if(str11>str22){
for(int i=0;i<str11-str22;i++){
str2="0"+str2;
}
}
else{
for(int i=0;i<str22-str11;i++){
str1="0"+str1;
}
}
str11=str1.length();
int cmp=0;
int cf=0;
for(int i=str11-1;i>=0;i--){
cmp=str1[i]-'0'+str2[i]-'0'+cf;
cf=cmp/10;
cmp=cmp%10;
s=char(cmp+'0')+s;
}
if(cf!=0){
s=char(cf+'0')+s;
}
return s;
}
int main(){
string sum="0";
string str;
while(cin>>str){
if(str=="0"){
break;
}
sum=doo(sum,str);
}
cout<<sum<<endl;
return 0;
}
另外二种方法
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char a[200],b[200],c[200],d[200];
int main()
{
int n1,n2;
b[0]='0';
while(1){
scanf("%s",a);
if(strcmp(a,"0")==0){
printf("%s\n",b);
return 0;
}
int i;
int n1=strlen(a)-1;
int n2=strlen(b)-1;
int p=0;
for(i=0;n1>=0||n2>=0;i++,n1--,n2--)
{
if(n1>=0&&n2>=0){c[i]=a[n1]+b[n2]-'0'+p;}
if(n1>=0&&n2<0){c[i]=a[n1]+p;}
if(n1<0&&n2>=0){c[i]=b[n2]+p;}
p=0;
if(c[i]>'9'){c[i]=c[i]-10;p=1;}
}
if(p==1){
c[strlen(c)]='1';
}
for(int i=0;i<strlen(c);i++){
d[strlen(c)-i-1]=c[i];
}
strcpy(b,d);
}
}
——————————————————
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char a[200],b[200],c[200];
int main()
{
//int n1,n2;
b[0]='0';
while(~scanf("%s",a))
{
if(strcmp(a,"0")==0)
{
printf("%s\n",b);
return 0;
}
int i;
int n1=strlen(a)-1;
int n2=strlen(b)-1;
int p=0;
i=n1;
if(i<n2)
i=n2;
for(i; n1>=0||n2>=0; i--,n1--,n2--)
{
if(n1>=0&&n2>=0)
{
c[i]=a[n1]+b[n2]-'0'+p;
}
if(n1>=0&&n2<0)
{
c[i]=a[n1]+p;
}
if(n1<0&&n2>=0)
{
c[i]=b[n2]+p;
}
p=0;
if(c[i]>'9')
{
c[i]=c[i]-10;
p=1;
}
}
if(p==1)
{
for(i=strlen(c); i>0; i--)
c[i]=c[i-1];
c[0]='1';
}
strcpy(b,c);
}
}
POJ 1503 Integer Inquiry 简单大数相加的更多相关文章
- POJ 1503 Integer Inquiry(大数相加,java)
题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...
- POJ 1503 Integer Inquiry(大数相加)
一.Description One of the first users of BIT's new supercomputer was Chip Diller. He extended his exp ...
- Poj 1503 Integer Inquiry
1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS Memory Limit: 1000 ...
- UVa 424 Integer Inquiry 【大数相加】
解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当 ...
- POJ 1503 Integer Inquiry 大数 难度:0
题目链接:http://poj.org/problem?id=1503 import java.io.*; import java.math.BigInteger; import java.util. ...
- poj 1503 Integer Inquiry (高精度运算)
题目链接:http://poj.org/problem?id=1503 思路分析: 基本的高精度问题,使用字符数组存储然后处理即可. 代码如下: #include <iostream> # ...
- Integer Inquiry【大数的加法举例】
Integer Inquiry Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27730 Accepted: 10764 ...
- 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:679 解决:357 题目描述: One of the first users of BIT's new supercomputer was ...
- hdu 1047 Integer Inquiry(大数)
题意:整数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<stdlib.h> #include ...
随机推荐
- .NET设计模式(6):原型模式(Prototype Pattern)
):原型模式(Prototype Pattern) ); //使用颜色 string colorName = "red"; C ...
- angularjs中异常处理
1.TypeError: Cannot read property '$valid' of undefined a. Add ng-submit attribute to the form: < ...
- Could not parse mapping document from resource cn/spt/model/Student.hbm.xml
初始hibernate, 写第一个程序 helloworld的错误: Exception in thread "main" org.hibernate.InvalidMapping ...
- Synchronized vs SyncRoot
我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot实例方法,他们之间有联系吗?我怎么才能用好他们呢?我们以Hash ...
- SQL Server中Id自增列的最大Id是多少
什么是自增列 在SQL Server中可以将Id列设为自增.即无需为Id指定值,由SQL Server自动给该列赋值,每新增一列Id的值加一,初始值为1. 需要注意的是即使将原先添加的所有数据都删除, ...
- javascript 用函数实现“继承”
一.知识储备: 1.枚举属性名称的函数: (1)for...in:可以在循环体中遍历对象中所有可枚举的属性(包括自有属性和继承属性) (2)Object.keys():返回数组(可枚举的自有属性) ( ...
- CSS3中的background-size(对响应性图片等比例缩放)
background-size的基本属性 background-size: 可以设定背景图像的尺寸,该属性是css3中的,在移动端使用的地方很多,比如最常见的地方在做响应性布局的时候,比如之前做的项目 ...
- C#程序中:如何向xml文件中插入节点(数据)
向xml文件中动态的添加节点(数据)是一件很爽的事,可以给你的程序带来很多的方便,比如在web中,如果你的Flash用到了xml文件,这个方法可以让你在后台就轻轻松松的更新你的Flash内容哦!一起研 ...
- Android新建项目 默认布局改为 LinearLayout
目前此方法仅适用于eclipse 需要修改SDK 目录 android-sdk/tools/templates/activities/BlankActivity/root/res/layout 文件: ...
- EntityFramework 和 linq 判断是否在指定时间段内的方法
EntityFramework: System.Data.Objects.EntityFunctions.DiffDays(DateTime.Now, inputTime)判断当前时间与指定时间相差多 ...