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 ...
随机推荐
- 概述ASP.NET缓存机制
PetShop之ASP.NET缓存机制 如果对微型计算机硬件系统有足够的了解,那么我们对于Cache这个名词一定是耳熟能详的.在CPU以及主板的芯片中,都引入了这种名为高速缓冲存储器(Cache)的技 ...
- html 中设置span的width完美解决方法
在默认情况下,由于span是行标签,设置width是无效的.只有改变display的属性,才可以实现设置宽度. 1.初步想法 span{ background-color:#ccc; display: ...
- C语言之分配
#include "stdio.h" void main() { ] = {,,}; *sizeof(int));//malloc calloc relloc arr2[] = ; ...
- 关于C#的那点事........
起源 C#(读做C-sharp)编程语言是由微软公司的Anders Hejlsberg和 Scott Willamette领导的开发小组专门为.NET平台设计的语言,它可以使程序员移植到.NET上.这 ...
- WF学习笔记(四)
1.使用WorkflowApplication.OnUnhandledException 处理Code异常 ,用于产生异常的Activity: public class ExceptionActivi ...
- Go语言探险思考笔记
最近接触对象存储,国际上鼎鼎有名的Amazon S3还有Google Cloud Service在国内由于防火墙还有机房过远的问题,并不能投入生产使用.国内有名的对象存储,大家众所周知的七牛云,后台就 ...
- linux上配置subversion服务器端安装配置并使用svn,windows本地检出,设置同步更新服务器的钩子
参考http://my.oschina.net/junn/blog/164041 http://songxj.blog.51cto.com/620981/396113 http://5iwww.blo ...
- 主流的phpcms分析
小型网站适合wordpress,onethink,joomla(囧啦) wordpress(免费开源) 优点:1.样式丰富,模板重多 2. 安全性 3. 对搜索引擎友好,收录快. ...
- 求LR(0)文法的规范族集和ACTION表、GOTO表的构造算法
原理 数据结构 // GO private static Map<Map<Integer,String>,Integer> GO = new HashMap<Map< ...
- [CSS]cursor鼠标样式
用css控制鼠标样式的语法如下: <span style="cursor:*">文本或其它页面元素</span> 把 * 换成如下15个效果的一种: ...