zoukankan      html  css  js  c++  java
  • 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 } 
  • 相关阅读:
    Java集合类总结 (三)
    Java集合类总结 (二)
    Java集合类总结 (一)
    发布方配ASP.NET网站服务器
    Ubuntu重启搜狗输入法
    常用期刊检索
    latex 写大论文图目录中图注过长解决方案
    understanding backpropagation
    Ubuntu安装Adobe Reader
    【转】pdf文件自动切白边
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8289770.html
Copyright © 2011-2022 走看看