zoukankan      html  css  js  c++  java
  • SZU:B47 Big Integer II

    Judge Info

    • Memory Limit: 32768KB
    • Case Time Limit: 10000MS
    • Time Limit: 10000MS
    • Judger: Normal

    Description

    Please calculate the answer of A*B, both A and B are integer.

    Input

    The first line of input contains T(1 leq T leq 1000), the number of test cases. There is only line for each test case. It contains two integers A,B(-10^{100} leq A,B leq 10^{100}).

    Output

    For each test case, output A*B in one line.

    Sample Input

    3
    1 2
    1 1
    -1 -1
    

    Sample Output

    2
    1
    1

    Ps : 算法课的小练习,通过字符串处理。注意0就OK了。

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 char A[200];
     5 char B[200];
     6 int C[200+200+1];
     7 
     8 char *revstr(char *str, int len){
     9     char *start = str;
    10     char *end = str + len - 1;
    11     char ch;
    12     if(str != 0){
    13         while(start < end){
    14             ch = *start;
    15             *start ++ = *end;
    16             *end-- = ch;
    17         }
    18     }
    19     return str;
    20 }
    21 
    22 int main(int argc, char const *argv[])
    23 {
    24     int t, i, j, lenA, lenB, len, Amark, Bmark, mark;
    25     scanf("%d", &t);
    26     while(t--){
    27 
    28         mark=0;
    29         Amark = 0;
    30         Bmark = 0;
    31 
    32         memset(A, '0', sizeof(A));
    33         memset(B, '0', sizeof(B));
    34         memset(C, 0, sizeof(C));
    35         scanf("%s", A);
    36         scanf("%s", B);
    37         lenA = strlen(A);
    38         lenB = strlen(B);
    39 
    40         if(A[0] == '-'){
    41             for(i=1;i<lenA;++i)
    42                 A[i-1] = A[i];
    43             A[i-1]= '';
    44             A[i] = '0';
    45             Amark = 1;
    46             lenA--;
    47         }
    48 
    49         if(B[0] == '-'){
    50             for(i=1;i<lenB;++i)
    51                 B[i-1] = B[i];
    52             B[i-1] = '';
    53             B[i] = '0';
    54             Bmark = 1;
    55             lenB--;
    56         }
    57         revstr(A,lenA);
    58         revstr(B,lenB);
    59         A[lenA] = '';
    60         B[lenB] = '';
    61         
    62         for(i=0;i<lenB;++i){
    63             for(j=0;j<lenA;++j){
    64                 C[j+i]+=(B[i]-'0')*(A[j]-'0');
    65             }    
    66         }
    67         len = lenA + lenB;
    68         for(i=0;i<len;++i){
    69             if(C[i] > 9){
    70                 C[i+1]+=C[i]/10;
    71                 C[i] %= 10;
    72             }
    73         }
    74          while(!C[len-1]){
    75                  len--;
    76          }
    77          //printf("len = %d
    ", len);
    78          if(len == 0){ printf("0
    "); continue;}
    79          if(len > -1 && Amark != Bmark)
    80              putchar('-');
    81         for(i=len-1;i>=0;i--)
    82             printf("%d", C[i]);
    83         
    84         printf("
    ");
    85     }
    86 
    87     return 0;
    88 }
  • 相关阅读:
    《Dubbo》接入与调用流程
    SpringBoot自定义配置实现与解析
    Docker使用笔记记录
    Elasticsearch(一):介绍与安装
    索引的建立与使用规范
    RocketMQ(一):介绍与安装
    NETCORE使用MINIO的PRESIGNEDPUTOBJECTASYNC方法,实现通过浏览器直接上传图片到MINIO服务器
    Minio 使用.NET + Vue 实现断点续传、秒传
    VS Code中小程序与Vue常用插件合集(前端合集)
    [瞎想笔记] 浅浅浅谈期望
  • 原文地址:https://www.cnblogs.com/firstrate/p/3320958.html
Copyright © 2011-2022 走看看