zoukankan      html  css  js  c++  java
  • UVa 424 Integer Inquiry 【大数相加】

    解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数。

    反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果。

     Integer Inquiry 

    One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3 to go from 0 to 333 and he explored taking various sums of thosenumbers.

    ``Thissupercomputer is great,'' remarked Chip. ``I only wish Timothy were here to seethese results.'' (Chip moved to a new apartment, once one became available onthe third floor of the Lemon Sky apartments on Third Street.)

    Input

    The input willconsist of at most 100 lines of text, each of which contains a singleVeryLongInteger. Each VeryLongInteger will be 100 or fewer characters inlength, and will only contain digits (no VeryLongInteger will be negative).

    The final inputline will contain a single zero on a line by itself.

    Output

    Your programshould output the sum of the VeryLongIntegers given in the input.

    Sample Input

    123456789012345678901234567890

    123456789012345678901234567890

    123456789012345678901234567890

    0

    Sample Output

    370370367037037036703703703670

     

    #include<stdio.h>
    #include<string.h>
    #define max 1000
    void add(char a[],char b[],char c[])
    {
        char m[max],n[max];
        int i;
        int len1 ,len2;
        int flag=0;
        memset(m,0,sizeof(m));
        memset(n,0,sizeof(n));
        len1=strlen(a);
        len2=strlen(b);
        for(i=0;i<len1;i++)
        {
            m[i]=a[len1-i-1]-'0';
        }
        for(i=0;i<len2;i++)
        {
            n[i]=b[len2-i-1]-'0';
        }
        for(i=0;i<=len1||i<=len2;i++)
        {
            c[i]=m[i]+n[i]+flag;
            flag=c[i]/10;
            c[i]=c[i]%10+'0';
        }
    }
    void shuchu(char c[])
    {
        int i,j;
        int len;
        len=strlen(c);
        for(i=len-1;c[i]=='0';i--);
        for(j=i;j>=0;j--)
        {
            printf("%c",c[j]);
        }
        printf("
    ");
    }
    int main()
    {
        char a[max],b[max],c[max];
        int i,j,len,tag=1;
         memset(b,0,sizeof(b));
         for(j=1;j<200&&tag;j++)
         {
             scanf("%s",&a);
             if(a[0]=='0')
             tag=0;
             memset(c,0,sizeof(c));
            add(a,b,c);
            len=strlen(c);
            for(i=0;i<len;i++)
            {
                b[i]=c[len-i-1];
            }
         }
         shuchu(c);
    
    }
    

      

  • 相关阅读:
    Codeforces Beta Round #92 (Div. 2 Only) B. Permutations 模拟
    POJ 3281 Dining 最大流 Dinic算法
    POJ 2441 Arrange the BUlls 状压DP
    URAL 1152 Faise Mirrors 状压DP 简单题
    URAL 1039 Anniversary Party 树形DP 水题
    URAL 1018 Binary Apple Tree 树形DP 好题 经典
    pytorch中的forward前向传播机制
    .data()与.detach()的区别
    Argparse模块
    pytorch代码调试工具
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4117869.html
Copyright © 2011-2022 走看看