HDU-1047,Integer Inquiry(大数加法)

简介: HDU-1047,Integer Inquiry(大数加法)

Problem 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.



This problem contains multiple test cases!


The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.


The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input:


1


123456789012345678901234567890


123456789012345678901234567890


123456789012345678901234567890


0


Sample Output:


370370367037037036703703703670


类似的可以看这篇博客:https://blog.csdn.net/weixin_43823808/article/details/98449076


程序代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define N 101
int num[N],sum[N];
char s[N];
void find(char s[])
{
  int len=strlen(s),j=0;
  memset(num,0,sizeof(num));
  for(int i=len-1;i>=0;i--)
    num[j++]=s[i]-'0';
  for(int i=0;i<N;i++)
  {
    sum[i]+=num[i];
    if(sum[i]>=10)
    {
      sum[i]-=10;
      sum[i+1]++;
    }
  }
}
int main()
{
  int n;
  cin>>n;
  while(n--)
  {
    memset(sum,0,sizeof(sum));
    while(cin>>s)
    {
      if(strcmp(s,"0")==0)
        break;
      find(s);
    }
    bool flag=false;
    for(int i=N;i>=0;i--)
    {
      if(flag)
        cout<<sum[i];
      else if(sum[i])
      {
        cout<<sum[i];
        flag=true;
      }
    }
    if(!flag)
      cout<<"0";
    cout<<endl;
    if(n)
      cout<<endl;
  }
  return 0;
}


相关文章
|
1月前
|
Go
Integer Inquiry(UVA—424)
Integer Inquiry(UVA—424)
LeetCode 343. Integer Break
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
57 0
LeetCode 343. Integer Break
HDOJ(HDU) 1708 Fibonacci String
HDOJ(HDU) 1708 Fibonacci String
79 0
|
Go
POJ 1503 Integer Inquiry 简单大数相加
POJ 1503 Integer Inquiry 简单大数相加
68 0
hdu 3336 Count the string
点击打开链接hdu 3336 思路:kmp+next数组的应用 分析: 1 题目要求的是给定一个字符串s,求字符串s的所有的前缀在s的匹配的次数之和mod10007. 2 很明显n1,为什么要从n开始而不是1开始呢,这里因为是要求前缀的匹配数而不是后缀; 4 求sum的时候注意每一步都有可能超过范围,所以就要求一次sum同时取模一次。
836 1
|
Java 编译器
[LeetCode]Reverse Integer题解
题目链接:7. Reverse Integer 难度:Easy Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 N...
761 0
[LeetCode] Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n
1182 0
LeetCode - 7. Reverse Integer
7. Reverse Integer Problem's Link  ---------------------------------------------------------------------------- Mean:  将一个整数的数值位反转.
793 0