• Simple Arithmetics


    http://poj.org/problem?id=1396

    挺坑的一道题,调试了一天跪在了格式上,要注意的情况很多,样例太没有代表性了,很容易理解错格式。。

    要注意的情况:(1)(加法、减法) 水平线 '----' 的数量为它相邻两侧字符串的最大长度。

                        (2)(乘法) 当第二个数为一位数时,’----'的数量同(1); 否则输出两次’----‘,最上面的’-----‘数量同(1),下面的’----‘数量为最后结果的长度。

                        (3)计算减法的时候,结果要去掉前导0。

                        (4)注意右对齐,每组数据后有一个空行。

    样例:

    (1)  999            1000          100    (2)  123      1240       (3)      8888
          +11                -1           -98            *4        *20                -8880
           ----                ---           ---           ---          ---                   -----
          1010              999             2          492            0                       8

                                                                        2480

                                                                        -----

                                                                        24800

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <algorithm>
      4 #include <string.h>
      5 const int N=100010;
      6 using namespace std;
      7 char s1[N]= {'0'},s2[N]= {'0'};
      8 char s11[N],s22[N],s33[N];
      9 char str[N];
     10 char s[550][1010];
     11 char sd[550][1010];
     12 int m = 0,len = 0;
     13 void add(char *st1,char *st2,int len)//大数加法
     14 {
     15     int j;
     16     for (j = strlen(st1); j < len; j++)
     17         st1[j] = '0';
     18     st1[j] = '';
     19     for (j = strlen(st2); j < len; j++)
     20         st2[j] = '0';
     21     st1[j] = '';
     22     int c = 0,t = 0;
     23     for (int i = 0; i < len; i++)
     24     {
     25         int temp = st1[i]-'0'+st2[i]-'0'+c;
     26         str[t++] = temp%10+'0';
     27         c = temp/10;
     28     }
     29     if (c!=0)
     30         str[t++] = c+'0';
     31     str[t]='';
     32     reverse(str,str+t);
     33 }
     34 void sub(char *st1,char *st2)//大数减法
     35 {
     36     int j,len=strlen(st1);
     37     for (j = strlen(st2); j < len; j++)
     38         st2[j] = '0';
     39     st1[j] = '';
     40     int c = 0,t = 0;
     41     for (int i = 0; st1[i]; i++)
     42     {
     43         int temp;
     44         if(st1[i]-'0'==0)
     45         {
     46             if(c==1)
     47             {
     48                 temp = 9-(st2[i]-'0');
     49                 c = 1;
     50             }
     51             else
     52             {
     53                 if(st2[i]-'0'!=0)
     54                 {
     55                     temp = 10-(st2[i]-'0');
     56                     c = 1;
     57                 }
     58                 else
     59                 {
     60                     temp = 0;
     61                     c = 0;
     62                 }
     63             }
     64         }
     65         else
     66         {
     67             if(st1[i]-'0'-c >= st2[i]-'0')
     68             {
     69                 temp = st1[i]-'0'-c-(st2[i]-'0');
     70                 c = 0;
     71             }
     72             else
     73             {
     74                 temp = st1[i]-'0'+10-c-(st2[i]-'0');
     75                 c = 1;
     76             }
     77 
     78         }
     79         str[t++] = temp%10+'0';
     80     }
     81     str[t]='';
     82     reverse(str,str+t);
     83 }
     84 void mul(char* st1, int b)//大数与一位数乘法
     85 {
     86     int c = 0,t = 0;
     87     if (b==0)
     88         strcpy(str,"0");
     89     else
     90     {
     91         for (int i = 0; st1[i]; i++)
     92         {
     93             int temp = (st1[i]-'0')*b+c;
     94             str[t++] = temp%10+'0';
     95             c = temp/10;
     96         }
     97         if (c!=0)
     98             str[t++] = c+'0';
     99         str[t] = '';
    100         reverse(str,str+t);
    101     }
    102     strcpy(s[m],str);
    103     strcpy(sd[m++],str);
    104 }
    105 inline void show(int len1,int len2,char* str1,char ch)//格式输出
    106 {
    107     len = len1 > len2+1?len1:len2+1;
    108     int len3 = strlen(str1);
    109     int MaxLen = len > len3?len:len3;
    110     for (int i = 0; i < MaxLen-len1; i++)
    111         printf(" ");
    112     puts(s11);
    113     for(int i = 0; i < MaxLen-len2-1; i++)
    114         printf(" ");
    115     putchar(ch);
    116     puts(s22);
    117     len = len2+1>len3?len2+1:len3;
    118     for (int i = 0; i < MaxLen-len; i++)
    119         printf(" ");
    120     for (int i = 0; i < len; i++)
    121         printf("-");
    122     puts("");
    123     for (int i = 0; i < MaxLen-len3; i++)
    124         printf(" ");
    125     puts(str1);
    126 }
    127 int main()
    128 {
    129     //freopen("data1.in", "r", stdin);
    130     //freopen("sad.out", "w", stdout);
    131     int T;
    132     scanf("%d",&T);
    133     while(T--)
    134     {
    135         m = 0;
    136         int p2 = 0,pos = 0;
    137         char ss[N],oper;
    138         scanf("%s",ss);
    139         for (int i = 0; ss[i]; i++)
    140         {
    141             if (ss[i]=='+'||ss[i]=='-'||ss[i]=='*')
    142             {
    143                 oper=ss[i];
    144                 pos = i;
    145                 break;
    146             }
    147             else
    148                 s1[i]=ss[i];
    149         }
    150         s1[pos]='';
    151         strcpy(s2,ss+pos+1);
    152         p2 = strlen(ss)-pos-1;
    153         strcpy(s11,s1);
    154         strcpy(s22,s2);
    155         reverse(s1,s1+pos);
    156         reverse(s2,s2+p2);
    157         len = pos>p2?pos:p2;
    158         if (oper=='+')
    159         {
    160             add(s1,s2,len);
    161             show(pos,p2,str,'+');
    162         }
    163         else if(oper=='-')
    164         {
    165             sub(s1,s2);
    166             int gps,pp = 0;
    167             for (int i = 0; str[i]; i++)
    168             {
    169                 if(str[i]!='0')
    170                 {
    171                     gps = i;
    172                     break;
    173                 }
    174             }
    175             for (int i = gps; str[i]; i++)
    176             {
    177                 str[pp++] = str[i];
    178             }
    179             str[pp] = '';
    180             show(pos,p2,str,'-');
    181         }
    182         else if (oper=='*')
    183         {
    184             int Mlen = len;
    185             mul(s1,s2[0]-'0');
    186             strcpy(ss,s[0]);
    187             for (int i = 1; s2[i]; i++)
    188             {
    189                 mul(s1,s2[i]-'0');
    190                 int lens = strlen(s[i]);
    191                 for (int j = 0; j < i; j++)
    192                     s[i][lens++] = '0';
    193                 s[i][lens] = '';
    194                 Mlen=max(strlen(s[i]),strlen(ss));
    195                 reverse(s[i],s[i]+strlen(s[i]));
    196                 reverse(ss,ss+strlen(ss));
    197                 add(s[i],ss,Mlen);
    198                 strcpy(ss,str);
    199             }
    200             Mlen = strlen(ss);
    201             if (Mlen==p2)
    202                 Mlen=p2+1;
    203             if(strlen(s22)==1)
    204                 show(pos,p2,ss,'*');
    205             else
    206             {
    207                 for (int i = 0; i < Mlen-pos; i++)
    208                     printf(" ");
    209                 puts(s11);
    210                 for (int i = 0; i < Mlen-p2-1; i++)
    211                     printf(" ");
    212                 printf("*");
    213                 puts(s22);
    214                 len = p2+1>strlen(sd[0])?p2+1:strlen(sd[0]);
    215                 for (int i = 0; i < Mlen-len; i++)
    216                     printf(" ");
    217                 for (int i = 0; i < len; i++)
    218                     printf("-");
    219                 puts("");
    220                 for (int i = 0; i < m; i++)
    221                 {
    222                     int d = Mlen-strlen(sd[i])-i;
    223                     for (int j = 0; j < d; j++)
    224                         printf(" ");
    225                     printf("%s
    ",sd[i]);
    226                 }
    227                 for (int i = 0; i < Mlen-strlen(ss); i++)
    228                     printf(" ");
    229                 for(int i = 0; i < strlen(ss); i++)
    230                     printf("-");
    231                 puts("");
    232                 for (int i = 0; i < Mlen-strlen(ss); i++)
    233                     printf(" ");
    234                 puts(ss);
    235             }
    236         }
    237         puts("");
    238     }
    239     return 0;
    240 }
    View Code
  • 相关阅读:
    shiro实战系列(三)之架构
    shiro实战系列(二)之入门实战续
    ShopNC B2B2C多用户商城2014商业版,带微商城
    开源 SHOPNC B2B2C结算营运版 wap IM客服 API 手机app 短信通知
    PHP5.3下加速器ZendGuardLoader安装 (LNMP/lnmpa)
    XAMPP + Xdebug+Zend Studio
    magento 12 配置安装教程
    C#控件一览表
    GetXamarin.xambe
    新建电子监控点与测速点
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3551074.html
Copyright © 2020-2023  润新知