zoukankan      html  css  js  c++  java
  • 问题 C: A+B Problem II

    题目描述

    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

    A,B must be positive.

    输入

    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

    输出

    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation.

    样例输入

    2
    1 2
    112233445566778899 998877665544332211

    样例输出

    Case 1:
    1 + 2 = 3
    Case 2:
    112233445566778899 + 998877665544332211 = 1111111111111111110
     1 /*对于此题做法有两种:
     2  其一,使2字符串的中的字符数字减去'0',逐个相加大于等于10的可以使本位减10,下一位自增1,后面的处理就非常简单了;
     3  其二,便是读入字符串后先让各个字符减'0',一一对应存入整形数组中;之后再相加。
     4     对于2种方法大致是相同的,都要从后面向前加,逢十进位,以及数组初始化均要初始为0,一边方便运算。
     5  下面用法一,代码如下:*/
     6 #include<stdio.h>
     7 #include<string.h>
     8 
     9 int main( ) {
    10     int n, i,len1, len2, j, k, pi, t;
    11     char str1[1010], str2[1010];
    12     scanf("%d", &n);
    13     for(i = 1; i <= n; i++) {
    14         int a[1200] = {0};
    15         int flag = 0;
    16         scanf("%s%s", str1, str2);//以字符串形式读入
    17         printf("Case %d:
    ", i);
    18         len1 = strlen(str1);
    19         len2 = strlen(str2);
    20         printf("%s + %s = ", str1, str2);
    21         j = len1-1;
    22         k = len2-1;
    23         pi = 0;
    24         while(j >= 0 && k >= 0) { //开始相加
    25             if(a[pi] + (str1[j]-'0') + (str2[j]-'0') >= 10) { //相加后大于10的
    26                 a[pi] = a[pi] + (str1[j] -'0')+(str2[k] - '0')-10;
    27                 a[pi+1]++;
    28             }
    29             else
    30                 a[pi] = a[pi] + (str1[j] - '0')+(str2[k]-'0');
    31             pi++; k--; j--;
    32         }
    33         if(j>=0) {
    34             for(t = j; t >= 0; t--) {
    35                 a[pi] = a[pi]+(str1[t]-'0');
    36                 pi ++;
    37             }
    38         }
    39         else if(k >= 0) {
    40             for(t = k; t >= 0; t--) {
    41                 a[pi] = a[pi] + str2[t]-'0';
    42                 pi++;
    43             }
    44         }
    45         else if(a[pi]!=0)//对于位数相同2个数加后最高位大于10的
    46             pi++;
    47         for(t = pi - 1; t >= 0; t--) {
    48             if(a[t]==0&&flag==0)
    49                 continue;
    50             else
    51             {
    52                 flag=1;
    53                 printf("%d", a[t]);
    54             }
    55         }
    56         if(i!=n)//对于2组之间加空行的情况
    57             printf("
    ");
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    接口中可以定义内部类举例
    eclipse中项目maven update无法停止解决方法
    tomcat failed to start / could not publish server configuration解决
    导入项目时jsp带红叉,或者实现类提示需要override接口的问题
    常见异常与总结——一句话的描述
    Maven导入shiro的依赖后,欢迎页面404、shiro-features异常的解决方案:
    eclipse解决maven项目右键run as没有run on server的问题:
    SpringCloud搭建教程
    异常:java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx' in value "${xxx}"
    SQL题1——查询所有购入商品为两种或两种以上的购物人记录
  • 原文地址:https://www.cnblogs.com/tong69/p/5785457.html
Copyright © 2011-2022 走看看