zoukankan      html  css  js  c++  java
  • 整数加法

    整数加法

     

    Problem Description

    题目描述
    小明上小学,数学老师教了正整数加法,但是这个正整数有点大,要算出两个整数的和,小明很是头痛
    ,现要你帮忙编写一个程序帮小明实现两个整数的相加。正整数位数在1-500位之间。

    Input

    本题有多个测试用例
    输入两个整数,每个整数占一行。

    Output

    输出两个整数和。

    Sample Input

    1
    2
    345
    78

    Sample Output

    3
    423

    解释:

    见代码吧,好像没有什么难度。

     1 #include<bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 const int N = 510;
     6 
     7 int main () {
     8 
     9   int num_one[N], num_two[N];
    10   int num[N];
    11   char str_one[N], str_two[N];
    12   while (cin >> str_one >> str_two) {
    13     int len_one = strlen(str_one);
    14     int len_two = strlen(str_two);
    15 
    16     for (int i = 0; i < len_one; i++) {
    17       num_one[i] = str_one[len_one - i - 1] - '0';
    18     }
    19 
    20     for (int i = 0; i < len_two; i++) {
    21       num_two[i] = str_two[len_two - i - 1] - '0';
    22     }
    23 
    24     int len = 0;
    25     int intger = 0;
    26     while (len < len_one && len < len_two) {
    27       num[len] = num_one[len] + num_two[len] + intger;
    28       intger = num[len] / 10;
    29       num[len] = num[len] % 10;
    30       len++;
    31     }
    32 
    33     while (len < len_one) {
    34       num[len] = num_one[len] + intger;
    35       intger = num[len] / 10;
    36       num[len] = num[len] % 10;
    37       len++;
    38     }
    39 
    40     while (len < len_two) {
    41       num[len] = num_two[len] + intger;
    42       intger = num[len] / 10;
    43       num[len] = num[len] % 10;
    44       len++;
    45     }
    46 
    47     if (intger != 0) {
    48       num[len++] = intger;
    49     }
    50 
    51     for (int i = len-1; i >= 0; i--) {
    52       printf("%d", num[i]);
    53     }
    54     printf("
    ");
    55   }
    56   return 0;
    57 }
    View Code
  • 相关阅读:
    不同浏览器的JS如何兼容?
    过滤器如何配置(javax.servlet.Filter)?
    hibernate中 dialect,lazy,inverse,cascade属性的用途?
    json注记
    php: $$str
    MySql计算字段的长度
    封装一个获取变量准确类型的函数
    JavaScript如何创建一个对象
    python+selenium自动登录163邮箱
    获取cookie
  • 原文地址:https://www.cnblogs.com/gznb/p/11207974.html
Copyright © 2011-2022 走看看