• hdu Integer Inquiry 解题报告


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047

    题目意思:就是求大整数加法。有多个案例,每个案例之间要输出一个空格。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int maxn = 10000 + 10;
     8 char t[maxn];
     9 int s[maxn], ans[maxn];
    10 
    11 int main()
    12 {
    13     int l1, l2, n, i, j, l, len, num;
    14     scanf("%d", &n);
    15     while (n--)
    16     {
    17         num = 0;
    18         memset(ans, 0, sizeof(ans));
    19         while (cin >> t && strcmp(t, "0"))
    20         {
    21             memset(s, 0, sizeof(s));
    22             l1 = strlen(t);
    23             for (i = 0; i < l1; i++)
    24                 s[i] = t[l1-i-1] - '0';
    25             num++;
    26             if (num == 1)
    27             {
    28                 for (i = 0; i < l1; i++)
    29                     ans[i] = s[i];
    30                 l2 = l1;
    31             }
    32             else
    33             {
    34                 len = max(l1, l2);  // l1:s[i] l2:ans[i]
    35                 l = min(l1, l2);
    36                 int sum, c = 0;
    37                 for (i = 0; i < len; i++)
    38                 {
    39                     if (i < l)
    40                     {
    41                         sum = s[i] + ans[i] + c;
    42                         ans[i] = sum % 10;
    43                         c = (sum > 9 ?  1 : 0);
    44                     }
    45                     else
    46                     {
    47                         if (l1 == len)
    48                             sum = s[i] + c;
    49                         else
    50                             sum  = ans[i] + c;
    51                         ans[i] = sum % 10;
    52                         c = (sum > 9 ?  1 : 0);
    53                     }
    54                 }
    55                 if (c == 1 && i == len)
    56                 {
    57                     ans[len] = 1;
    58                     l2 = len + 1;
    59                 }
    60                 else
    61                     l2 = len;   // 关键之处!!!!之前不记得更新错了好多次!!!
    62             }
    63         }
    64         if (!num)
    65             printf("0");
    66         else
    67         {
    68             for (i = maxn-1; i >= 0; i--)
    69             {
    70                 if (ans[i])
    71                     break;
    72             }
    73             for ( ; i >= 0; i--)
    74                 printf("%d", ans[i]);
    75         }
    76         printf("
    ");
    77         if (n)
    78             cout << endl;
    79     }
    80     return 0;
    81 }

      忽略格式问题,借鉴了别人的写法

       

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 const int maxn = 1e5 + 10;
     8 char t[maxn];
     9 int num[maxn];
    10 
    11 void add(char s[])
    12 {
    13     int z = 0;
    14     int len = strlen(s);
    15     for (int i = len-1; i >= 0; i--)
    16     {
    17         num[z] += (s[i] - '0');
    18         num[z+1] += num[z] / 10;
    19         num[z] %= 10;
    20         z++;
    21     }
    22 }
    23 
    24 int main()
    25 {
    26     int i, ok = 0;
    27     memset(num, 0, sizeof(num));
    28     while (scanf("%s", t) != EOF && strcmp(t, "0"))
    29         add(t);
    30     for (i = maxn; i >= 0; i--)
    31     {
    32         if (num[i])
    33         {
    34             ok = 1;
    35             break;
    36         }
    37     }
    38     for ( ; i >= 0 && ok; i--)
    39          printf("%d", num[i]);
    40     if (!ok)
    41         printf("0");
    42     printf("
    ");
    43     return 0;
    44 }
  • 相关阅读:
    UVA 11806 组合数学+容斥
    Educational Codeforces Round 37 (Rated for Div. 2) G
    java 5 线程池
    团队-象棋游戏-项目进度
    结对-五子棋-测试过程
    结对-五子棋游戏-开发过程
    课后作业-阅读任务-阅读提问-2
    20171006-构建之法:现代软件工程-阅读笔记
    结队-五子棋游戏-项目进度
    团队-象棋游戏-代码设计规范
  • 原文地址:https://www.cnblogs.com/windysai/p/3643981.html
Copyright © 2020-2023  润新知