zoukankan      html  css  js  c++  java
  • 4.2.1 B

    Description

    The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

    Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

    ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).


    Input



    The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.


    Output



    For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.


    Sample Input



    3
    24 1
    4358 754
    305 794


    Sample Output



    34
    1998
    1

    思路简单就是简单的将其放在数组中,然后在相加,注意边界为零的情况,但是在测试库里没有001,即第一个数位零的情况

    #include <iostream>
    #include <cstring>
    #include <string>       //string类型length
    using namespace std;
    int a[1000]={0},b[1000]={0},c[1000]={0};
    int zhuanhuan(string str,bool boo)
    {
     int n,i,l=0, w=0;;
     n=str.length();
     if(boo==0)
      for(i=0;i<n;i++)//适用于整数
      {
       if(!(w==0&&str[i]=='0'))
       {
        w=1;
        a[l]=str[i]-'0';

        l++;
       }
      }
     else
      for(i=0;i<n;i++)//适用于整数
       if(!(w==0&&str[i]=='0'))
       {
        w=1;
        b[l]=str[i]-'0';
        l++;
       }
     return l;
    }
    int main()
    {
       int n,i,j;
       string str;
       cin>>n;
       for(i=1;i<=n;i++)
       {
         memset(a,0,sizeof(int)*1000);
      memset(b,0,sizeof(int)*1000);
      memset(c,0,sizeof(int)*1000);
         cin>>str;
     int x;
     x=zhuanhuan(str,0);
      cin>>str;
     int y;
     y=zhuanhuan(str,1);
     int weishu=x>y?x:y;
      for(j=0;j<weishu;j++)
      {
       c[j]=a[j]+b[j]+c[j];
       if(c[j]>9)
       {
        c[j]=c[j]-10;
          c[j+1]++;
       }
      }
      int ww=0;

      if(c[weishu]!=0)
      weishu++;
      for(j=weishu-1;j>0;j--)
       if(c[j]==0)
        c[j]=-1;
       else 
        break;
      for(j=0;j<weishu;j++)
               if(!(ww==0&&c[j]==0||c[j]==-1))
         {
         ww=1;
         cout<<c[j];
         }
         cout<<endl;
       }

    return 0;
    }

    超流比代码:

    运用倒置字符串函数reverse函数:

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int main(void)
    {
     int t;
     scanf("%d",&t);
     while(t--)
     {
      int a,b;
      char s1[13],s2[13];
      scanf("%d%d",&a,&b);
      sprintf(s1,"%d",a);
      sprintf(s2,"%d",b);
      reverse(s1,s1+strlen(s1));
      reverse(s2,s2+strlen(s2));
      sscanf(s1,"%d",&a);
      sscanf(s2,"%d",&b);
      sprintf(s1,"%d",a+b);
      reverse(s1,s1+strlen(s1));
      sscanf(s1,"%d",&a);
      printf("%d ",a);
     }
     return 0;
    }

  • 相关阅读:
    将Ajax 中数组转换成字符串 封装成类
    网页中删除数据弹出提示框
    pdo连接数据库
    pdo 的配置与启用
    php中常用的运算符
    [JZOJ 5912] [NOIP2018模拟10.18] VanUSee 解题报告 (KMP+博弈)
    [JZOJ 5910] [NOIP2018模拟10.18] DuLiu 解题报告 (并查集+思维)
    [JZOJ 5852] [NOIP2018提高组模拟9.6] 相交 解题报告 (倍增+LCA)
    [JZOJ 5437] [NOIP2017提高A组集训10.31] Sequence 解题报告 (KMP)
    [JZOJ 5875] [NOIP2018提高组模拟9.20] 听我说,海蜗牛 解题报告(BFS+二分)
  • 原文地址:https://www.cnblogs.com/zswbky/p/5432136.html
Copyright © 2011-2022 走看看