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 ...
随机推荐
- JS关闭窗口或JS关闭页面的几种代码
//JS定时自动关闭窗口 <script language="javascript"> <!-- function closewin(){ self.opener ...
- Angularjs总结(五)指令运用及常用控件的赋值操作
1.常用指令 <div ng-controller="jsyd-controller"> <div style="float:left;width:10 ...
- XML解析(转)
非原创,转载自http://my.oschina.net/khakiloveyty/blog/396671 xml解析主要可以使用CData,libxml2以及NSXMLParser,以下对各个方法给 ...
- iOS中判断消息推送是否打开
根据 [[UIApplication sharedApplication] enabledRemoteNotificationTypes] 的返回值来进行判断,该返回值是一个枚举值,如下: typed ...
- CABasicAnimation添加动画离开屏幕就动画停止的问题
解决方法: animation.removedOnCompletion = NO;
- Input的readonly 属性与disabled属性
readonly 不可编辑,可以获得焦点,背景颜色默认灰色,值的字体颜色默认为灰色,值可以在请求中传递 disabled 不可编辑,不可以获得焦点,背景颜色默认灰色,值的字体颜色默认为灰色,值不可以在 ...
- windows上SVN服务器以及客户端TortoiseSVN的安装配置
(1)svn的安装 1. 下载软件:Setup-Subversion-1.7.5.msi,安装就很容易了,一路NEXT 2. 把SVN的bin文件夹路径添加到环境变量中 把svn安装目录下的bin路径 ...
- Windows phone 之Image控件
wp目前支持的图片格式为png和jpeg ,我们可以通过设置Source属性设置图片源. 还有两个属性是:Stretch,Opacity Stretch属性 image的拉伸行为有此属性决定,此属性是 ...
- jQuery入门[3]-事件
jQuery对事件的支持主要包括: bind()--为事件绑定处理程序,如: $("p").bind("mouseenter mouseleave", func ...
- Spring MVC 统一异常处理
Spring MVC 统一异常处理 看到 Exception 这个单词都心慌 如果有一天你发现好久没有看到Exception这个单词了,那你会不会想念她?我是不会的.她如女孩一样的令人心动又心慌,又或 ...