Integer Inquiry(UVA—424)

简介: Integer Inquiry(UVA—424)

题目:

One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration ofpowers 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 Skyapartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInte-ger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (noVeryLongInteger 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

<font color=red Sample Output

370370367037037036703703703670

解题思路:这个题是高精度的问题,多个大数相加的结果.

程序代码:

#include<stdio.h>
#include<string.h>
int a[50000],b[50000],c[55000];
char s[200];
int main()
{
  int n,m,i,j,k;
  memset(a,0,sizeof(a));
  scanf("%s",s);
  m=strlen(s);
  j=0;
  for(i=m-1;i>=0;i--)
    a[j++]=s[i]-'0';
  while(scanf("%s",s)!=EOF)
  {
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    if(s[0]=='0')
      break;
    n=strlen(s);
    j=0;
    for(i=n-1;i>=0;i--)
      b[j++]=s[i]-'0';
    if(n>m)
      k=n;
    else
      k=m;
    for(i=0;i<k;i++)
    {
      c[i]=a[i]+b[i]+c[i];
      if(c[i]>=10)
      {
        c[i+1]++;
        c[i]=c[i]%10;
      }
    }
    if(c[k]!=0)
      k++;
    for(i=0;i<k;i++)
      a[i]=c[i];
    m=k;  
  }
  for(i=m-1;i>=0;i--)
    printf("%d",a[i]);
  printf("\n");
  return 0; 
}













相关文章
UVa11076 - Add Again
UVa11076 - Add Again
56 0
HDU-1047,Integer Inquiry(大数加法)
HDU-1047,Integer Inquiry(大数加法)
uva 10706 - Number Sequence
点击打开链接uva 10706 题目意思:    有一个数组 s[1] = 1 , s[2] = 1 2 , .......s[k] = 1....k,要求给定一个n表示数组的第几位,要求这个第几位是什么数。
953 1
最大乘积(Maximum Product,UVA 11059)
Problem D - Maximum Product Time Limit: 1 second   Given a sequence of integers S = {S1, S2, .
906 0
uva 11462 Age Sort
点击打开链接uva 11462 水题 #include #include #include #include using namespace std; const int maxn = 2000010; int num[maxn]; i...
876 0
uva11076Add again
View Code 题意:给定n和n个数,求所有的不重复的全排列的对应数字的和。 分析:对于每个数字,在每一位出现的概率相同,那么只算出一位的结果即可。对于每一位,拿出这个数字后剩下的数字的结果,乘以这个数字对应的下标i那么就是权和了。。
633 0