POJ1503,Integer Inquiry,与hdu1047一样。下面加上另外几种方法

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:

#include
#include
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=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<

网上找的好代码,学习了。。。

code1:

#include 
#include
#include 
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=0;i--)
  printf("%d",sum[i]);
 printf("\n");
 return 0;
}
好代码2。。。

code2:

#include
#include
#include
#include
#include
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=0;--i,++j)
        {
            if(i==max&&sum_temp[i]=='0')   //检查并消去高位0
            {
                --max;
                continue;
            }
            sum[j]=sum_temp[i];         //倒置
        }
        sum[j]='\0';           //末尾添加结束符
    }
    cout<



你可能感兴趣的:(水题)