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;
    }

  • 相关阅读:
    数据库版本管理工具flyway
    spring webapp的配置文件放置在项目外的方法
    logback
    linux as4 bind9 设置进程中的一些小效果
    设置/勾销Debian的屏保
    Linux内存:内存管理的天禀
    用YUM晋级CentOS体系中PHP和MySQL
    solaris的故事
    Solaris 的防火墙ipfilter设置
    mysql安置设置文件的成绩
  • 原文地址:https://www.cnblogs.com/zswbky/p/5432136.html
Copyright © 2011-2022 走看看