12 条题解

  • 31
    @ 2023-8-1 20:18:04

    进制转换

    • 我们先翻译一下题目,让它看起来简洁一点。
    • 比较a进制的数x,与b进制的数y的大小,输出比较结果。
    • 是不是很简单。看题目不难发现,这其实就是进制转换的最基本应用,跟把2进制的1001转换成十进制没什么区别。
    • 下面我说说进制转换的方法。

    (1)a进制转10进制

    • 我们类比十进制每个位代表的数字,比如234,其中个位代表的数是4* 10^0,十位代表的是3 *10^1,而百位则代表2 *10^2。
    • 怎么样,发现了没有,十进制的数从后数第x位就代表 x位上的数 *10^x-1 ,我们可以将这个规律放到a进制上。
    • 就得到规律 a进制的数的倒数第x位表示 x位上的数*a^x-1.
    • 我们总结整理这个规律之后就可以得到a进制转10进制的方法。
    1. 输入字符串a(用字符串是因为题目只说了转换成十进制后<=2^32-1,但输入的有可能是二进制的30个1,这样就存不了了) 以及数x。
    2. 定义一个变量sum,用以储存转换成十进制后的数。之后遍历字符串,将a的第i位 * x的位数减一 次方。
    3. 遍历完成后输出即可。

    辅助理解代码:

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string x;
        int a,sum=0,cf=1;
        /*a为进制数,x为数字。
    sum为转化为十进制后的数,cf储存a^x-1。
        */
        cin>>a>>x;
        for (int i=x.length()-1;i>=0;i--)
        //从最后一位遍历到第一位。
        {
            sum+=(x[i]-'0')*cf;//就是上面讲的核心公式
            cf=cf*a;//cf的指数+1。
        }
        cout<<sum;//输出答案。
        return 0;
    }
    

    • 当你们完全你接上面代码之后,下面就简单了,因为AC代码和上面辅助理解的代码也大差不差。
    • 步骤:

    1. 将第一个数转化为十进制。(具体步骤参考上面)
    2. 将第二个数转化为十进制。(具体步骤参考上面)
    3. 比较大小。

    AC代码(解析在注释中)

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string x,y;
        int a,sum=0,cf=1,b,sum2=0,cf2=1;
        //a为进制数,x为数字。sum为转化为十进制后的数,cf储存a^x-1。
    
        //转换第一个数。
    
        cin>>a>>x;
        for (int i=x.length()-1;i>=0;i--)
        //从最后一位遍历到第一位。
        {
            sum+=(x[i]-'0')*cf;//就是上面讲的核心公式
            cf=cf*a;//cf的指数+1。
        }
    
        //转换第二个数。
    
        cin>>b>>y;
        for (int i=y.length()-1;i>=0;i--)
        //从最后一位遍历到第一位。
        {
            sum2+=(y[i]-'0')*cf2;//就是上面讲的核心公式
            cf2=cf2*b;//cf的指数+1。
        }
    
        //下面为比大小,输出答案。
        if (sum>sum2)
            cout<<"x>y";
        else if (sum<sum2)
            cout<<"x<y";
        else
            cout<<"x=y";
        return 0;
    }
    

    如果这篇题解对你有帮助,那请动动鼠标点个赞吧,真的写了很久,求求了。

    • 5
      @ 2022-11-18 10:57:00

      最基础的代码......

      #include <iostream>
      #include <cmath>
      using namespace std;
      //进制转换
      int to_decimalNum(int a , string n)
      {
          //a进制数n
          int p = 0 , de = 0 , i = n.length() - 1;
          while (i >= 0)
          {
              int d = pow(a , p);
              de += (n[i] - '0') % 10 * d;
              p++;
              i--;
          }
          return de;
      }
      
      int main()
      {
          short a , b;
          int decimalx , decimaly;
          string x , y;
          cin >> a >> x >> b >> y;
          decimalx = to_decimalNum(a , x);
          decimaly = to_decimalNum(b , y);
      
          if (decimalx == decimaly)
          {
              cout << "x=y";
          }
          else if (decimalx > decimaly)
          {
              cout << "x>y";
          }
          else
          {
              cout << "x<y";
          }
          return 0;
      }
      
      • 4
        @ 2022-8-3 19:42:33

        由于60%的数据中,a=b,所以在这60分数据中我们可以不进行进制转换,运用ASCII码从左到右依次判断字符串的每个字符的大小,最后得出结果。

        以下代码中,xx表示字符串x,yy表示字符串y,xlen与ylen分别表示字符串x、y的长度。 因为进制相等,所以比较的时候可以先比较字符串长度,较长的更大(应该不用解释吧?)。 flag表示是否已经判断完成。 如果两个字符串长度一样则从左到右依次判断每个字符的大小。 因为是数字字符,所以转换成整型的时候只需要 int(x-'0') 即可(x为0~9之间的字符)。

        int mian(){
            ios::sync_with_stdio(false);
            cin.tie(0);
            cout.tie(0);
            cin >> a >> xx;
            cin >> b >> yy;
            if (a == b && a <= 10) {
                int xlen = xx.length(), ylen = yy.length();
                if (xlen > ylen) {
                    cout << "x>y\n";
                }
                else if (xlen < ylen) {
                    cout << "x<y\n";
                }
                else {
                    flag = false;
                    for (int i = 0; i < xlen; i++) {
                        if (int(xx[i] - '0') > int(yy[i] - '0')) {
                            cout << "x>y\n";
                            flag = true;
                            break;
                        }
                        else if (int(xx[i] - '0') < int(yy[i] - '0')) {
                            cout << "x<y\n";
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)cout << "x=y\n";
                }
            }
            else {
        
            }
            return 0;
        }
        

        满分做法则可在上方代码的return 0上的else中加入进制转换。

                int xlen = xx.length(), ylen = yy.length();
                for (int i = xlen - 1; i >= 0; i--) {
                    x += int(xx[i] - '0') * pow(a, xlen - i - 1);
                }
                for (int i = ylen - 1; i >= 0; i--) {
                    y += int(yy[i] - '0') * pow(b, ylen - i - 1);
                }
                if (x > y) {
                    cout << "x>y\n";
                }
                else if (x == y) {
                    cout << "x=y\n";
                }
                else {
                    cout << "x<y\n";
                }
        

        该方法时间复杂度为O(xx.length() + yy.length()),递交后所有样例的用时均小于1ms。

        • 3
          @ 2024-1-24 19:15:33

          AC啦:

          #include <bits/stdc++.h>
          #define ll long long
          #define re register int
          using namespace std;
          int main()
          {
              ios::sync_with_stdio(false);
              cin.tie(0);
              cout.tie(0);
              ll a, b;
              string x, y;
              cin >> a >> x >> b >> y;
              ll sum1 = 0, sum2 = 0, cnt1 = 0, cnt2 = 0;
              for (re i = x.length() - 1; i >= 0; i--)
              {
                  sum1 += (x[i] - '0') * pow(a, cnt1);
                  cnt1++;
              }
              for (re i = y.length() - 1; i >= 0; i--)
              {
                  sum2 += (y[i] - '0') * pow(b, cnt2);
                  cnt2++;
              }
              //cout << sum1 << " " << sum2 << endl;
              if (sum1 == sum2)
                  cout << "x=y" << endl;
              else if (sum1 < sum2)
                  cout << "x<y" << endl;
              else
                  cout << "x>y" << endl;
              return 0;
          }
          
          • 3
            @ 2022-10-11 19:45:53

            #HT1058. 比较大小 题解

            前言

            嗨嗨嗨,我又来啦!!!
            这是一道非常水的题

            思路

            进制转换(很简单吧)+比大小(也很简单吧)

            AC Code(防抄袭)

            #include <bits/stdc++.h>
            #define int long long
            using namespace std;
            int a, b, x, y;
            string s1, s2;
            signed main()
            {
            	cin >> a >> s1 >> b >> s2;
            	for (int i = 0; i < s1.size(); i++) x = x * a + s1[i] - '0';
            	for (int i = 0; i < s2.size(); i++) y = y * b + s2[i] - '0';
            	cout << (x == y ? "x=y" : (x < y ? "x<y" : "x>y"));
            	return 0;
            }
            
            • 3
              @ 2022-9-24 9:44:29
              #include <cstdio>
              
              long long x , y;
              //题目范围:2^31-1
              //不开long long 真的会没
              
              inline short usual_reader(){
              	//基础版快读 
              	//不超过10进制,short够了 
              	short it = 0 , chr = getchar() ;
              	while('0' > chr || chr > '9')
              		chr = getchar() ;
              	while('0' <= chr && chr <= '9')
              		it = (it << 3) + (it << 1) + chr - '0' , chr = getchar() ;
              	return it ;
              }
              
              inline long long NB_reader(short a){ 
              	//自定义的可读取其他进制的更NB的更快的输入的函数 
              	//甚至可以读取36进制
              	//看完了题目才发现就10进制 
              	long long it = 0 , chr = getchar();
              	while(('0' > chr || chr > '9') && ('A' > chr || chr > 'Z'))
              		chr = getchar() ;
              	while(('0' <= chr && chr <= '9') || ('A' <= chr && chr <= 'Z'))
              		it = it * a + (
              			('0' <= chr && chr <= '9') ? chr - '0' : chr - 'A' + 10
              		) , chr = getchar() ;
              	//其实就是把原来的改了一点点
              	//乘10 ==> 乘a
              	//判断若chr是字符 就把chr - '0' 改为 chr - 'A' + 10 
              	return it ;
              }
              
              int main(){
              	x = NB_reader(usual_reader());
              	y = NB_reader(usual_reader());
                  //获取的直接是转换后的数
                  //所以直接判断
                  //putchar() 貌似更快点
              	putchar('x') ;
              	if(x > y)
              		putchar('>') ;
              	if(x == y)
              		putchar('=') ;
              	if(x < y)
              		putchar('<') ;
              	putchar('y') ;
              	return 0 ;
              }
              
              • 3
                @ 2021-8-7 14:50:30

                这里讲解一下如何直接进行进制转换:

                首先最熟悉的2进制转10进制

                比如二进制数字:11001

                我们从右往左,依次是

                202^0*1+212^1*0+222^2*0+232^3*1+242^4*1

                也就是 1+0+0+8+16=25

                再看一个三进制转10进制:20121

                我们从右往左,依次是

                303^0*1+313^1*2+323^2*1+333^3*0+343^4*2

                也就是1+6+9+0+162=178

                此题的方法就是将数字转换成十进制再进行比较

                转换的时候注意要从后往前遍历字符串,然后求a的x次方我们可以用pow(a,x)

                cin >> a >> x;
                	cin >> b >> y;
                	for(int i=x.length()-1;i>=0;i--)
                	{
                		xx += (x[i]-'0')*pow(a,x.length()-1-i);
                	}
                	for(int i=y.length()-1;i>=0;i--)
                	{
                		yy += (y[i]-'0')*pow(b,y.length()-1-i);
                	}
                	if(xx>yy)
                	{
                		cout << "x>y";
                	}
                	if(xx<yy)
                	{
                		cout << "x<y";
                	}
                	if(xx==yy)
                	{
                		cout << "x=y";
                	}
                
                • 2
                  @ 2023-7-10 16:34:52

                  最简单的

                  #include<bits/stdc++.h>
                  using namespace std;
                  int a,b,nowx,nowy;
                  string x,y;
                  int main(){
                      cin>>a>>x;
                      cin>>b>>y;
                      for(int i=x.length()-1;i>=0;i--)
                          nowx+=pow(a,x.length()-1-i)*(x[i]-'0');//感谢老师
                      for(int i=y.length()-1;i>=0;i--)
                          nowy+=pow(b,y.length()-1-i)*(y[i]-'0');
                      if(nowx<nowy)
                          cout<<"x<y";
                      if(nowx>nowy)
                          cout<<"x>y";
                      if(nowx==nowy)
                          cout<<"x=y";
                      return 0;
                  }
                  </span>
                  • 1
                    @ 2024-4-19 22:23:14
                    #include<bits/stdc++.h>
                    #define int long long
                    using namespace std;int a,b,x,y;string s1,s2;signed main(){cin>>a>>s1>>b>>s2;for(int i=0;i<s1.size();i++){x=x*a+s1[i]-'0';}for(int i=0;i<s2.size();i++)y=y*b+s2[i]-'0';cout<<(x==y?"x=y":(x<y?"x<y":"x>y"));}
                    
                    • 1
                      @ 2023-10-7 20:54:03
                      #include <bits/stdc++.h>
                      using namespace std;
                      string x,y;
                      int a,b;
                      int sum,sum1;
                      int main(){
                      	cin>>a>>x;
                      	int cnt=1,ans=0;
                      	for (int i=x.length()-1;i>=0;i--){
                      		ans+=(x[i]-'0')*cnt;
                      		cnt=cnt*a;
                      	}//模拟转换 
                      	sum=ans;
                      	cin>>b>>y;
                      	cnt=1,ans=0;
                      	for (int i=y.length()-1;i>=0;i--){
                      		ans+=(y[i]-'0')*cnt;
                      		cnt=cnt*b;
                      	}
                      	sum1=ans;//赋值 
                      	if (sum>sum1){
                      		cout<<"x>y";
                      	}
                      	else if (sum<sum1){
                      		cout<<"x<y";
                      	}
                      	else{
                      		cout<<"x=y";
                      	}//判断 
                      	return 0;
                      }
                      
                      • 1
                        @ 2023-8-16 14:31:15
                        #include<bits/stdc++.h>
                        using namespace std;
                        int a,b,nowx,nowy;
                        string x,y;
                        int main(){
                            cin>>a>>x;
                            cin>>b>>y;
                            for(int i=x.length()-1;i>=0;i--)
                                nowx+=pow(a,x.length()-1-i)*(x[i]-'0');
                            for(int i=y.length()-1;i>=0;i--)
                                nowy+=pow(b,y.length()-1-i)*(y[i]-'0');
                            if(nowx<nowy)
                                cout<<"x<y";
                            if(nowx>nowy)
                                cout<<"x>y";
                            if(nowx==nowy)
                                cout<<"x=y";
                            return 0;
                        }/、已AC
                        
                        • -1
                          @ 2023-2-6 15:31:16

                          写题解请注意

                          鼓励大家写题解,但注意题解格式。

                          题解一定要有思路解析或代码注释,能否让别人理解你的思路

                          也是你的能力的检验,不要只放无意义的代码给大家复制,那就失去了做题的初心。

                          给代码两端加上这个会舒服一些

                          ```cpp

                          你的代码

                          ```

                          </span>

                          这个点在键盘的左上角tab上面那个键,注意切换输入法

                          #include<iostream>
                          using namespace std;
                          int main()
                          {
                              int n;
                              cin>>n;//这是一个注释
                              return 0;
                          }
                          

                          请注意严禁抄袭题解,写题解不要只放代码,需加上你的思路或代码注释。

                          抄袭题解一经发现直接取消成绩。

                          题解被删除的可能

                          1. 代码不符合格式规范
                          2. 没有思路讲解或者没有注释,
                          3. 无意义的题解

                          大家携手共同维护一个良好的编程环境,如果一经发现,多次作乱。可能会被管理员拉黑,请注意,一旦拉黑即失去登陆资格。

                          • 1

                          信息

                          ID
                          1190
                          时间
                          1000ms
                          内存
                          256MiB
                          难度
                          5
                          标签
                          递交数
                          939
                          已通过
                          333
                          上传者