• POJ 1503 Integer Inquiry


    http://poj.org/problem?id=1503

    题意:
    给出n个数,计算和。

    思路:

    基础的大整数加法题。

     1 #include <iostream>  
     2 #include <cstdio>  
     3 #include <cstring>  
     4 #include <algorithm>  
     5 #include <cmath>  
     6 #include<vector>
     7 using namespace std;
     8 
     9 const int maxn = 100 + 5;
    10 
    11 char s[maxn][maxn];
    12 int num[maxn][maxn];
    13 int ans[maxn];    
    14 int max_len;
    15 int n;
    16 
    17 int add()
    18 {
    19     int i;
    20     int t = 0;
    21     for (i = 0; i < max_len; i++)
    22     {
    23         int k = 0;
    24         for (int j = 0; j < n; j++)
    25         {
    26             k += num[j][i];
    27         }
    28         k += t;
    29         ans[i] = k % 10;
    30         t = k / 10;
    31     }
    32     while (t)
    33     {
    34         ans[i++] = t % 10;
    35         t /= 10;
    36     }
    37     return i;
    38 }
    39 
    40 int main()
    41 {
    42     //freopen("D:\txt.txt", "r", stdin);
    43     n = 0;
    44     max_len = 0;
    45     memset(num, 0, sizeof(num));
    46     while (gets(s[n]))
    47     {
    48         if (!strcmp(s[n], "0"))  break;
    49         int len = strlen(s[n]);
    50         max_len = max(max_len, len);
    51         for (int i = len - 1; i >= 0; i--)
    52             num[n][len - 1 - i] = s[n][i] - '0';
    53         n++;
    54     }
    55     int cnt = add();
    56     for (int i = cnt - 1; i >= 0;i--)
    57         printf("%d", ans[i]);
    58     printf("
    ");
    59     return 0;
    60 }
  • 相关阅读:
    IGV解读
    box-cox解读
    linux命令eval的用法
    R中导入excel乱码的解决办法
    Django下实现HelloWorld
    python的list求和与求积
    win10下安装Django
    python下实现汉诺塔
    (stm32f103学习总结)—DS18B20
    (stm32f103学习总结)—GPIO结构
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/6659718.html
Copyright © 2020-2023  润新知