2020杭电多校第六场—【1002 Little Rabbit‘s Equation】

【1002 Little Rabbit’s Equation】

又是模拟,模拟,模拟……
什么时候模拟我能一遍过呢
我可太离谱了

题意就是给我们一个 【数字 运算符(+ - * /) 数字 等号 数字】这么样的式子,然后让我们判断几进制的时候这个式子是对的

我去找了一个任意进制转十进制的自定义函数,真太好用了!然后就是把运算符左边的、右边的以及等号右边的提出来,再套循环转进制判断(要是有这么一个进制就输出,没有就-1)

真的不难啊 怎么就不能一遍过呢

坑点
1.进制都是从二开始的,并且我们的进制应该比每一位的数字都大
(进制从2开始!!连wa六发) 就是0+0=0 输出应该是2.
2.还有就是开ll 这应该问题不大。

Problem Description
Little Rabbit is interested in radix. In a positional numeral system, the radix is the number of unique digits, including the digit 0, used to represent numbers. For example, for the decimal system (the most common system in use today) the radix is ten, because it uses the ten digits from 0 to 9. Generally, in a system with radix b (b>1), a string of digits d1…dn denotes the number d1bn−1+d2bn−2+⋯+dnb0, where 0≤di

Little Rabbit casually writes down an equation. He wonders which radix this equation fits.

Input
The are several test cases. Each test case contains a string in a line, which represents the equation Little Rabbit writes down. The length of the string is at most 15. The input is terminated by the end-of-file.

The equation’s format: number, operator, number, =, number. There’s no whitespace in the string.

Each number has at least 1 digit, which may contain digital numbers 0 to 9 or uppercase letters A to F (which represent decimal 10 to 15). The number is guaranteed to be a non-negative integer, which means it doesn’t contain the radix point or negative sign. But the number may contain leading zeros.

The operator refers to one of +, −, ∗, or /. It is guaranteed that the number after / will not be equal to 0. Please note that the division here is not integer division, so 7/2=3 is not correct.

Output
For each test case, output an integer r (2≤r≤16) in a line, which means the equation is correct in the system with radix r. If there are multiple answers, output the minimum one. If there is no answer between 2 and 16, output −1.

Sample Input
1+1=10
18-9=9
AA*AA=70E4
7/2=3

Sample Output
2
10
16
-1

代码:

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;

    typedef long long ll;
    ll a,b,sum;
    int main()

    {

     int trans(char a[],int n);

     char strupr(char);

     char arr[100];
     char left[100],right[100],last[100];
     while(cin>>arr)
     {

//(得清,不然出大问题)  
   memset(left,0,sizeof(left));memset(right,0,sizeof(right));memset(last,0,sizeof(last));
     ll l=strlen(arr);
     ll p1,p2;
     int max=0;
     for(int i=0;i<l;i++)   //找出运算符和等号的位置
     {

         if(arr[i]=='+'||arr[i]=='-'||arr[i]=='*'||arr[i]=='/')
            {
                p1=i;
                continue;
            }
         if(arr[i]=='=')
            {
                p2=i;
                continue;
            }
        if(arr[i]>arr[max])
            max=i;
     }
     if(arr[max]>='0'&&arr[max]<='9')
        max=arr[max]-'0';
     else
        max=arr[max]-'A'+10;
        max+=1;
        if(max==1) //就是这里,进制问题注意一下
        //max的意思本来是找到每一位中最大的那个但是0+0=0(进制是2))
        //不特判就会出来0+1(害!)
            max=2;

     //cout<
     int j=0;
     for(int i=0;i<l;i++)   //然后把他们提出来就left right last
     {
         if(i<p1)
         {
                left[j]=arr[i];
                j++;
         }
         if(i==p1)
            j=0;
         if(i>p1&&i<p2)
         {
          right[j]=arr[i];
            j++;
         }
         if(i==p2)
            j=0;
         if(i>p2)
         {
             last[j]=arr[i];
             j++;
         }
     }
     ll x=0,y=0,z=0;
     int t=0;
     for(int i=max;i<=16;i++)
     {
        x = trans( left , i );  
         //转进制(int trans(char chs[],int m),前面是要转的字符串,后面是这个字符串是几进制的)
        y = trans( right , i );
        z = trans( last , i );
       //cout<<"i:"<
       //cout<<"x:"<
        if(arr[p1]=='*'&&x*y==z)
        {
                printf("%d\n",i);
                t=1;
                break;

        }
         if(arr[p1]=='-'&&x-y==z)
        {
                printf("%d\n",i);
                t=1;
                break;
        }
         if(arr[p1]=='/'&&(double)x/(double)y==(double)z)   //(double注意一下)
        {


                printf("%d\n",i);
                t=1;
                break;

        }
         if(arr[p1]=='+'&&x+y==z)
        {
                printf("%d\n",i);
                t=1;
                break;
        }

     }
     if(t==0)
        printf("-1\n");
           memset(arr,0,sizeof(arr));
     }

     return 0;

    }

   //下面就都是自定义函数啦,其实不转大小写也没事,题目输入就是全大写的

    char strupr(char ch)//小写字母转换为大写字母

    {

      if(ch>='a' && ch<='z')

      {

       ch = ch - 32;

      }

     // printf("%c",ch);

      return ch;

    }



    int trans(char chs[],int m)//转换为十进制

    {

     char *p = chs;

     int len = strlen(p);

     int temp,sum=0;

     while(len>0)

     {

      char ss = strupr(*p);

      if(ss>='0' && ss<='9')

      {

        temp = (ss-'0')*pow(m,len-1);//m表示进制类型(2、8、16)

        sum = sum + temp;

      }

      if(ss>='A' && ss<='F')

      {

        temp = (ss-'A'+10)*pow(m,len-1);

        sum = sum + temp;

      }

      p++;

      len--;

     }

     //printf("\n");

     return sum;

    }

最后希望大家模拟都能一次过
(这不有手就行)

你可能感兴趣的:(模拟)