zoukankan      html  css  js  c++  java
  • 杭电acm1047

    http://acm.hdu.edu.cn/showproblem.php?pid=1047

    这题,,,,只是简单的大数相加,只要模拟小学时候算术就可以了,首先,个位对齐,接着,逐个相加,最后进位,其实相对应的减法也是如此,这题还有一个坑,就是要注意首位0的情况。。。。。

    #include<stdio.h>
    #include<string.h>
    int sum[300];
    int main()
    {
        int t,i,j,max;
        char s[300];
        scanf("%d",&t);
        gets(s);
        while(t--)
        {
            for(i=0;i<120;i++)
              sum[i]=0;
            max=0;
            while(gets(s),strcmp(s,"0"))
            {
               if(max<strlen(s))
                 max=strlen(s);
               for(i=strlen(s)-1,j=0;i>=0;i--)
                 sum[j++]+=s[i]-'0';
            }
            i=0;
               while(i<max-1)
               {
                 if(sum[i]>9)
                  {
                      sum[i+1]+=sum[i]/10;
                      sum[i]%=10;
                  }
                  i++;
               }
               i=max;
            while(sum[i]==0&&i>0)
            i--;
            
            for(;i>=0;i--)
                 printf("%d",sum[i]);
               printf("\n");
            if(t)
              printf("\n");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    LeetCode
    LeetCode
    控制反转(Ioc)
    KMP算法
    *&m与m的区别
    函数指针与函数指针数组的使用方法
    C++四种类型转换
    内存分配:堆内存,栈内存
    汇编 基础
    i++,++i 作为参数
  • 原文地址:https://www.cnblogs.com/huzhenbo113/p/3085334.html
Copyright © 2011-2022 走看看