zoukankan      html  css  js  c++  java
  • hdu 1047 Integer Inquiry

    Integer Inquiry

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 13978    Accepted Submission(s): 3524


    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
     
    普通:
     
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<stdlib.h>
     6 #include<queue>
     7 using namespace std;
     8 int main()
     9 {
    10     //freopen("in.txt","r",stdin);
    11     int n,sum[110];
    12     char s1[110];
    13     cin >> n;
    14     int i,j;
    15     while(n--)
    16     {
    17         getchar();
    18         memset(sum,0,sizeof(sum));
    19         while(scanf("%s",s1))
    20         {
    21             if(strcmp(s1,"0")==0)
    22             break;
    23             for(i=j=strlen(s1)-1;i>=0;i--)
    24             {
    25                 sum[j-i]+=s1[i]-'0';
    26             }
    27         }
    28         int c=0,x;
    29         for(i=0;i<110;i++)
    30         {
    31             x=sum[i]+c;
    32             sum[i]=x%10;
    33             c=x/10;
    34         }
    35         i=100;
    36         while(sum[i]==0)
    37         i--;
    38         if(i<0)
    39         cout << "0";
    40         for(;i>=0;i--)
    41         printf("%d",sum[i]);
    42         printf("
    ");
    43         if(n)
    44         printf("
    ");
    45     }
    46     return 0;
    47 }
    View Code

     模板:

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <math.h>
      5 #include <assert.h>
      6 #include <ctype.h>
      7 #include <map>
      8 #include <string>
      9 #include <set>
     10 #include <bitset>
     11 #include <utility>
     12 #include <algorithm>
     13 #include <vector>
     14 #include <stack>
     15 #include <queue>
     16 #include <iostream>
     17 #include <fstream>
     18 #include <list>
     19 using  namespace  std;
     20 
     21 const  int MAXL=120;
     22 struct  BigNum
     23 {
     24     int  num[MAXL];
     25     int  len;
     26     BigNum()
     27     {
     28         memset(num,0,sizeof(num));
     29     }
     30 };
     31 //高精度加法
     32 BigNum  Add(BigNum &a, BigNum &b)
     33 {
     34     BigNum c;
     35     int  i, len;
     36     len = (a.len > b.len) ? a.len : b.len;
     37     memset(c.num, 0, sizeof(c.num));
     38     for(i = 0; i < len; i++)
     39     {
     40         c.num[i] += (a.num[i]+b.num[i]);
     41         if(c.num[i] >= 10)
     42         {
     43             c.num[i+1]++;
     44             c.num[i] -= 10;
     45         }
     46     }
     47     if(c.num[len])
     48         len++;
     49     c.len = len;
     50     return  c;
     51 }
     52 void  print(BigNum &a)   //输出大数
     53 {
     54     int  i;
     55     for(i = a.len-1; i >= 0; i--)
     56         printf("%d", a.num[i]);
     57     puts("");
     58 }
     59 
     60 void Init(BigNum &a, char *s, int &tag)   //将字符串转化为大数
     61 {
     62     int  i = 0, j = strlen(s);
     63     if(s[0] == '-')
     64     {
     65         j--;
     66         i++;
     67         tag *= -1;
     68     }
     69     a.len = j;
     70     for(; s[i] != ''; i++, j--)
     71         a.num[j-1] = s[i]-'0';
     72 }
     73 
     74 int main(void)
     75 {
     76     //freopen("in.txt","r",stdin);
     77     int n;
     78     scanf("%d",&n);
     79     char s[MAXL];
     80     while(n--)
     81     {
     82         getchar();
     83         BigNum a;
     84         int tag=1;
     85         Init(a,"0",tag);
     86         while(scanf("%s",s))
     87         {
     88             BigNum b;
     89             if(strcmp(s,"0")==0)
     90             break;
     91             int tag=1;
     92             Init(b,s,tag);
     93             a=Add(a,b);
     94         }
     95         print(a);
     96         if(n)
     97         printf("
    ");
     98     }
     99     return 0;
    100 }
    View Code
     
  • 相关阅读:
    js遍历不要使用index 而是使用id(数据唯一表示)
    eureka
    Mybatis-plus自动填充字段的值(如createTime,UpdateTime)
    计算机网络入门
    操作系统入门
    计算机组成原理入门
    《事实》读书笔记
    推荐算法入门
    源码编译安装apache2.4脚本
    Mycat实现读写分离
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4334802.html
Copyright © 2011-2022 走看看