• Hdoj 1002


    问题描述

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

    输入描述

    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. Output a blank line between two test cases.

    样例输入
    2
    1 2
    112233445566778899 998877665544332211
    样例输出
    Case 1:
    1 + 2 = 3
     
    Case 2:
    112233445566778899 + 998877665544332211 = 1111111111111111110

    思路

    简单的大数问题

    代码

     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int main()
     5 {
     6     int n, j;
     7     scanf("%d", &n);
     8     for(j = 1; j <= n; j++) 
     9     {
    10         char ch1[1001], ch2[1001];
    11         scanf("%s", ch1);
    12         int s1 = strlen(ch1), i;
    13         int a[1001] = {0}, b[1001] = {0};
    14         for(i = s1-1; i >= 0; i--) { a[s1-i] = ch1[i] - '0'; }
    15         scanf("%s", ch2);
    16         int s2 = strlen(ch2);
    17         for(i = s2-1; i >= 0; i--) { b[s2-i] = ch2[i] - '0'; }
    18         printf("Case %d:
    %s + %s = ", j, ch1, ch2);
    19         int s = (s1>s2?s1:s2), temp=0;
    20         for(i = 1 ; i <= s; i++)
    21         {
    22             a[i] += b[i] + temp;
    23             temp = a[i] / 10;
    24             a[i] %= 10;
    25         }
    26         if(temp != 0) printf("%d",temp);
    27         for(i=s;i>0;i--) printf("%d",a[i]);
    28         if(j < n) printf("
    ");
    29         printf("
    ");
    30     }
    31 } 
  • 相关阅读:
    两个链表的第一个公共结点
    数组中的逆序对
    第一个只出现一次的字符(字符流中第一个只出现一次的字符)
    丑数
    最长不含有重复字符的子字符串
    礼物的最大价值
    把数字翻译成字符串
    把数组排成最小的数
    [CSP-S模拟测试]:赤(red)(WQS二分+DP)
    [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8289770.html
Copyright © 2020-2023  润新知