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

  • 相关阅读:
    童年记忆
    展现、通讯、IO
    通电自动开机
    英雄每多屠狗辈,自古侠女出风尘(看黄金大劫案有感)
    反射整理学习<一>(转)
    在ASP.NET中跟踪和恢复大文件下载
    高内聚、低耦合
    你需要权限才能执行此操作
    WP7应用开发笔记(5) 通信设计
    一个简单的软件工程流程
  • 原文地址:https://www.cnblogs.com/zswbky/p/5432136.html
Copyright © 2020-2023  润新知