• HDU 1002.A + B Problem II-数组模拟-大数相加


    A + B Problem II

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 435932    Accepted Submission(s): 84825


    Problem Description
    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
     
    Input
    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.
     
    Output
    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.
     
    Sample Input
    2
    1 2
    112233445566778899 998877665544332211
     
    Sample Output
    Case 1: 1 + 2 = 3
    Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
     
    Author
    Ignatius.L
     

    代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 int main(){
     4     char a[1005],b[1005];
     5     int c[1005],d[1005],ans[1005];
     6     int n,i,k,j,h,t,l;
     7     int len1,len2;
     8     scanf("%d",&n);
     9         h=0;
    10         while(n--){
    11             h+=1;
    12             for(i=0;i<1005;i++){
    13                 c[i]=d[i]=0;
    14                 ans[i]=0;
    15             }
    16             scanf("%s %s",&a,&b);
    17             len1=strlen(a);
    18             len2=strlen(b);
    19             for(i=0;i<len1;i++){
    20                c[i]=a[i]-'0';
    21             }
    22             for(i=0;i<len2;i++){
    23                d[i]=b[i]-'0';
    24             }
    25             printf("Case %d:
    ",h);
    26             for(i=0;i<len1;i++)
    27                 printf("%d",c[i]);
    28                 printf(" + ");
    29             for(i=0;i<len2;i++)
    30                 printf("%d",d[i]);
    31                 printf(" = ");
    32             for(i=0,j=len1-1;i<len1/2;i++,j--){
    33                 t=c[j];
    34                 c[j]=c[i];
    35                 c[i]=t;
    36             }
    37             for(i=0,j=len2-1;i<len2/2;i++,j--){
    38                 t=d[j];
    39                 d[j]=d[i];
    40                 d[i]=t;
    41             }
    42             l=len1>len2?len1:len2;
    43             for(i=0,k=0;i<=l;i++,k++){
    44                 if(c[i]+d[i]<10)
    45                     ans[k]=c[i]+d[i];
    46                 else{
    47                     ans[k]=(c[i]+d[i])%10;
    48                     c[i+1]+=(c[i]+d[i])/10;
    49                 }
    50 
    51             }
    52             for(j=k-1;j>=0;j--){
    53                 if(j==k-1&&ans[j]==0)
    54                     continue;
    55                 printf("%d",ans[j]);
    56             }
    57                 if(n==0)
    58                     printf("
    ");
    59                 else
    60                     printf("
    
    ");
    61         }
    62     return 0;
    63 }
  • 相关阅读:
    flask ajax
    python 符合条件跳过下一次循环
    python使用openpyxl excel 合并拆分单元格
    等价类划分法
    python 同级目录包导入问题,使用"."错误
    django:查询,反向查询
    Python实现程序执行次数的计数
    python 2x SSH通道连接服务器读取数据库和中文编码问题
    Python for 循环中使用append()添加可变元素,前面的值被覆盖,循环中内存应用地址不变
    以概率列表选择对应元素,轮盘概率选择Python实现
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9740218.html
Copyright © 2020-2023  润新知