POJ1503,Integer Inquiry,与hdu1047一样。下面加上另外几种方法
2012-10-16 07:44
405 查看
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
自己写的,比较挫。。。
code0:
网上找的好代码,学习了。。。
code1:
code2:
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
自己写的,比较挫。。。
code0:
#include<iostream> #include<string> using namespace std; #define MAX_LEN 100 int num1[MAX_LEN+10]; int num2[MAX_LEN+10]; char Str1[MAX_LEN+10]; char Str2[MAX_LEN+10]; void calculate() { int i,j; memset(num1,0,sizeof(num1)); memset(num2,0,sizeof(num2)); cin>>Str1>>Str2; bool IsFirst=true; while(Str2[0]!='0') { int Len1,Len2=strlen(Str2); if(IsFirst==true) { Len1=strlen(Str1); for(i=Len1-1,j=0;i>=0;i--) num1[j++]=Str1[i]-'0'; } for(i=Len2-1,j=0;i>=0;i--) num2[j++]=Str2[i]-'0'; for(i=0;i<MAX_LEN;i++) { num1[i]+=num2[i]; if(num1[i]>=10) { num1[i]-=10; num1[i+1]++; } } cin>>Str2; IsFirst=false; bool bStartOutPut=false; } bool bStartOutPut=false; for(i=MAX_LEN;i>=0;i--) { if(bStartOutPut) printf("%d",num1[i]); else if(num1[i]) { printf("%d",num1[i]); bStartOutPut=true; } } } int main() { int n; cin>>n; cin.get(); while(n--) { calculate(); cout<<endl; if(n) cout<<endl; } return 0; }
网上找的好代码,学习了。。。
code1:
#include <stdio.h> #include<string.h> #include <stdlib.h> char s[1001]; int sum[1002]; int main() { int i,j; while(gets(s)) { j=strlen(s); if(s[0]=='0'&&j==1) break; for(i=j-1;i>=0;i--) sum[j-i-1]+=(s[i]-'0'); } j=1001; while(!sum[j]) j--; for(i=0;i<j;i++) { sum[i+1]+=sum[i]/10; sum[i]=sum[i]%10; } for(i=j;i>=0;i--) printf("%d",sum[i]); printf("\n"); return 0; }好代码2。。。
code2:
#include<iostream> #include<cstring> #include<string> #include<cstdio> #include<cmath> using namespace std; const int large=1000; char sum_temp[large]; char digit_temp[large]; int Add(int j,int carry_bit) { int count; count=(sum_temp[j]-'0')+(digit_temp[j]-'0')+carry_bit; sum_temp[j]=count%10+'0'; if(count<10) return 0; else return 1; } int main(void) { int length,i,j,k; int max=0,carry_bit=0; char digit[large],sum[large]; memset(sum_temp,'0',sizeof(sum_temp)); for(i=-1;strcmp(digit,"0");) { i++; gets(digit); length=strlen(digit)-1; memset(digit_temp,'0',sizeof(digit_temp)); for(k=0,j=length;j>=0;--j,++k) digit_temp[k]=digit[j]; //倒置 if(max<length) max=length; for(carry_bit=0,j=0;j<=max;j++) //每两个长数相加一次进位必须初始化进位值 carry_bit=Add(j,carry_bit); if(carry_bit==1) //最高位进位检查 sum_temp[++max]='1'; for(i=max,j=0;i>=0;--i,++j) { if(i==max&&sum_temp[i]=='0') //检查并消去高位0 { --max; continue; } sum[j]=sum_temp[i]; //倒置 } sum[j]='\0'; //末尾添加结束符 } cout<<sum<<endl; return 0; }
相关文章推荐
- poj 1503 Integer Inquiry
- POJ 1503 Integer Inquiry 美丽的大整数相加 + 测试数据
- POJ 1503 && HDU 1047 Integer Inquiry(高精度)
- POJ 1503 Integer Inquiry
- POJ 1503 Integer Inquiry(大数加…
- POJ 1503 Integer Inquiry
- poj 1503 Integer Inquiry(多个大数相加)
- POJ--1503:Integer Inquiry 大数加法
- POJ 1503 Integer Inquiry
- UVA-424 POJ-1503 UVALive-5399 - Integer Inquiry
- POJ 1503 Integer Inquiry
- POJ1503 UVA424 UVALive5399 Integer Inquiry【大数】
- POJ 1503 Integer Inquiry 高精度加法
- POJ-1503-Integer Inquiry-2013-11-25 23:15:45
- Integer Inquiry POJ - 1503
- poj 1503 Integer Inquiry
- poj 1503 Integer Inquiry
- POJ 刷题系列:1503 Integer Inquiry
- POJ 1503 Integer Inquiry
- poj 1503 Integer Inquiry【高精度】