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 ...
随机推荐
- C#DbHelperOleDb,Access数据库帮助类 (转载)
主要功能如下数据访问抽象基础类 主要是访问Access数据库主要实现如下功能 .数据访问基础类(基于OleDb)Access数据库, .得到最大值:是否存在:是否存在(基于OleDbParameter ...
- android-satellite-menu
使用过Path的人都应该知道,在Path主界面的左下方有一个非常有意思的菜单.菜单由一个主按钮组成,当用户点击该按钮时,就会有一连串的按钮弹出,而Satellite Menu正是该菜单的一个开源版本. ...
- A题笔记(6)
No. 3040 代码量好少,主要考到数学知识 唯一需要注意的是变量的类型 int -2147483648 ~ +2147483647 (4 Bytes) long 在32位机器中 int 类型 和 ...
- oracle学习笔记3:基本的SQL语句
oracle基本的SQL语句和SQLSERVER基本一样,在这里只简单列出与SQLSERVER不一样的地方 1.select * from orderinfo where address = 'abc ...
- JAVA zip解压 MALFORMED 错误
最近在在使用zip 解压时,使用JDK1.7及以上版本在解压时,某些文件会报异常 Exception in thread "main" java.lang.IllegalArgum ...
- VS2008/MFC —常用控件使用总结 转载
在公司培训期间,经理给了我们没人10个界面草图,让我们在VS2008下使用MFC设计,因为在经理的帮助和自己的努力下,终于在三天时间内完成,现在就根据在这三天 时间里所用到的控件做出如下总结: 1.D ...
- MVVM模式应用 之NotificationObject类
public abstract class NotificationObject : INotifyPropertyChanged { public event PropertyChanged ...
- WF学习笔记(四)
1.使用WorkflowApplication.OnUnhandledException 处理Code异常 ,用于产生异常的Activity: public class ExceptionActivi ...
- jquery 效果
效果1.基本效果 1.1 show([speed,[easing],[fn]]) 如果元素本身是可见的,则不对其作任何改变.如果元素是隐藏的,则使其可见. $("p&qu ...
- js实现中文简繁切换效果
html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...