zoukankan      html  css  js  c++  java
  • [hdu 1002] A + B Problem II

    杭大的题,总是PE.

    原因:最后一行没有空行. - -|||.

    附上悠长的代码

    版本一

     1 #include <stdio.h>
     2 
     3 int main()
     4 {
     5     char A[1002], B[1002], C[1002];
     6     int nA, nB, nC;
     7     char *max, *min;
     8     char c;
     9     int maxLenght, minLenght;
    10 
    11     int N;
    12     scanf("%d
    ", &N);
    13     for (int index = 0; index < N; index++)
    14     {
    15         // read A
    16         for (nA = 0, c = getchar(); c != ' '; c = getchar(), nA++)
    17         {
    18             A[nA] = c;
    19         }
    20 
    21         // read B
    22         for (nB = 0, c = getchar(); c != '
    '; c = getchar(), nB++)
    23         {
    24             B[nB] = c;
    25         }
    26 
    27         // comput C
    28         if (nA > nB)
    29         {
    30             max = A;
    31             min = B;
    32             maxLenght = nA;
    33             minLenght = nB;
    34         }
    35         else
    36         {
    37             max = B;
    38             min = A;
    39             maxLenght = nB;
    40             minLenght = nA;
    41         }
    42         
    43         nC = 0;
    44         int reserve = 0, s, odd = maxLenght - minLenght;
    45 
    46         for (int i = minLenght - 1; 0 <= i; i--)
    47         {
    48             s = reserve + max[odd + i] + min[i] - 2 * '0';
    49             reserve = s / 10;
    50             C[minLenght - i - 1] = s - reserve * 10 + '0';
    51         }
    52 
    53         for (int i = odd - 1; 0 <= i; i--)
    54         {
    55             s = reserve + max[i] - '0';
    56             reserve = s / 10;
    57             C[maxLenght - i - 1] = s - reserve * 10 + '0';
    58         }
    59 
    60         if (reserve)
    61         {
    62             C[maxLenght] = reserve + '0';
    63             nC = maxLenght + 1;
    64         }
    65         else
    66         {
    67             nC = maxLenght;
    68         }
    69 
    70         // output
    71         printf("Case %d:
    ", index + 1);
    72         for (int i = 0 ; i < nA; i++)
    73         {
    74             putchar(A[i]);
    75         }
    76         printf(" + ");
    77         for (int i = 0 ; i < nB; i++)
    78         {
    79             putchar(B[i]);
    80         }
    81         printf(" = ");
    82         for (int i = nC - 1; 0 <= i; i--)
    83         {
    84             putchar(C[i]);
    85         }
    86 
    87         if (index < N - 1)
    88         {
    89             printf("
    
    ");
    90         }
    91         else
    92         {
    93             printf("
    ");
    94         }
    95     }
    96 
    97     return 0;
    98 }

    版本二,短了一点,输入输出的地方做了改进 - - 

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     char A[1002], B[1002], C[1002];
     7     int nA, nB, nC;
     8     char *max, *min;
     9     int maxLenght, minLenght;
    10 
    11     int N;
    12     scanf("%d
    ", &N);
    13     for (int index = 0; index < N; index++)
    14     {
    15         // read A B
    16         scanf("%s%s", A, B);
    17         nA = strlen(A);
    18         nB = strlen(B);
    19 
    20         // comput C
    21         if (nA > nB)
    22         {
    23             max = A;
    24             min = B;
    25             maxLenght = nA;
    26             minLenght = nB;
    27         }
    28         else
    29         {
    30             max = B;
    31             min = A;
    32             maxLenght = nB;
    33             minLenght = nA;
    34         }
    35         
    36         nC = 0;
    37         int reserve = 0, s, odd = maxLenght - minLenght;
    38 
    39         for (int i = minLenght - 1; 0 <= i; i--)
    40         {
    41             s = reserve + max[odd + i] + min[i] - 2 * '0';
    42             reserve = s / 10;
    43             C[minLenght - i - 1] = s - reserve * 10 + '0';
    44         }
    45 
    46         for (int i = odd - 1; 0 <= i; i--)
    47         {
    48             s = reserve + max[i] - '0';
    49             reserve = s / 10;
    50             C[maxLenght - i - 1] = s - reserve * 10 + '0';
    51         }
    52 
    53         if (reserve)
    54         {
    55             C[maxLenght] = reserve + '0';
    56             nC = maxLenght + 1;
    57         }
    58         else
    59         {
    60             nC = maxLenght;
    61         }
    62 
    63         // output
    64         printf("Case %d:
    %s + %s = ", index + 1, A, B);
    65         for (int i = nC - 1; 0 <= i; i--)
    66         {
    67             putchar(C[i]);
    68         }
    69         if (index < N - 1)
    70         {
    71             printf("
    
    ");
    72         }
    73         else
    74         {
    75             printf("
    ");
    76         }
    77     }
    78 
    79     return 0;
    80 }
  • 相关阅读:
    sqlserver表结构导出excel格式
    c#对象深复制demo
    aspose.cells导出Demo
    复制表结构数据至另一数据库
    C#安全类型转换基于convert
    c# winform devexpress TreeList过滤和绑定
    万能分页存储过程
    WebSocket简单使用
    本地搭建持续集成(AzureDevops)
    centos 7 安装nginx并启动(笔记)
  • 原文地址:https://www.cnblogs.com/night-ride-depart/p/4519435.html
Copyright © 2011-2022 走看看