3 条题解

  • 1
    @ 2023-11-23 20:51:44
    #include <iostream>
    
    #include <algorithm>
    
    #include <vector>
    
    #include <string>
    
    using namespace std;
    
    void change(int a,string s1,int b,string &s2)
    
    {
    
    vector<int>num;
    
    for(int i=0;i<s1.size();i++){
    
    if(s1[i]<='9') num.push_back(s1[i]-'0');
    
    else if(s1[i]<='Z') num.push_back(s1[i]-'A'+10);
    
    else num.push_back(s1[i]-'a'+36);
    
    }
    
    reverse(num.begin(),num.end());
    
    while(num.size()){
    
    int r=0;
    
    for(int i=num.size()-1;i>=0;i--){
    
    num[i]+=r*a;
    
    r=num[i]%b;
    
    num[i]/=b;
    
    }
    
    ans.push_back(r);
    
    while(num.size()&&num.back()==0)
    
    num.pop_back()
    
    }
    
    reverse(ans.begin(),ans.end());
    
    for(int i=0;i<ans.size();i++){
    
    if(ans[i]<=9) s2+=char(ans[i]+'0');
    
    else if(ans[i]<=35) s2+=char(ans[i]-10+'A');
    
    else s2+=char(ans[i]-36+'a');
    
    }
    
    }
    
    int main()
    
    {
    
    ios::sync_with_stdio(false);
    
    cin.tie(0);
    
    int t;
    
    cin>>t;
    
    while(t--){
    
    int a,b;
    
    string s1,s2;
    
    cin>>a>>b>>s1;
    
    change(a,s1,b,s2);
    
    cout<<a<<" "<<s1<<endl;
    
    cout<<b<<" "<<s2<<endl;
    
    cout<<endl;
    
    }
    
    return 0;
    
    }
    
    • 1
      @ 2021-8-7 19:00:37

      C++ :

      #include <iostream>
      #include <algorithm>
      #include <vector>
      #include <string>
      using namespace std;
       
      void change(int a,string s1,int b,string &s2)//将a进制的s1转换成b进制的s2
      {
          vector<int>num;
          for(int i=0;i<s1.size();i++){
              if(s1[i]<='9') num.push_back(s1[i]-'0');
              else if(s1[i]<='Z') num.push_back(s1[i]-'A'+10);
              else num.push_back(s1[i]-'a'+36);
          }
          reverse(num.begin(),num.end());//从低位到高位来存
          vector<int>ans;
          while(num.size()){//短除法,直到商0为止,即:num.empty
              int r=0;//r为每次短除法运算的余数
              for(int i=num.size()-1;i>=0;i--){//整个循环为一次短除法的运算,num中的数是从低位到高位来存的,倒过来从高位到底位符合除法顺序
                  num[i]+=r*a;//当前被除数等于((上一次的余数*a)+(当前位的数))<==>(转十进制))
                  r=num[i]%b;//取本次余数,作为下一次被除数那一部分的基数
                  num[i]/=b;//统计计算本次运算后num的值
              }
              ans.push_back(r);//将本次运算最后余数作为答案的这一位的值(这里是从低位到高位存的,因为短除法是逆序的)
              while(num.size()&&num.back()==0) num.pop_back();//去除前导零(正是因为vector只有pop_back没有pop_front,所以从低位到高位来存)
          }
          reverse(ans.begin(),ans.end());//因为ans是从低位到高位存的,所以最后要反过来
          for(int i=0;i<ans.size();i++){
              if(ans[i]<=9) s2+=char(ans[i]+'0');
              else if(ans[i]<=35) s2+=char(ans[i]-10+'A');
              else s2+=char(ans[i]-36+'a');
          }
      }
       
      int main()
      {
          ios::sync_with_stdio(false);
          cin.tie(0);
          int t;
          cin>>t;
          while(t--){
              int a,b;
              string s1,s2;
              cin>>a>>b>>s1;
              change(a,s1,b,s2);
              cout<<a<<" "<<s1<<endl;
              cout<<b<<" "<<s2<<endl;
              cout<<endl;
          }
          return 0;
      }
      
      • 0
        @ 2024-5-19 20:51:07

        既然这道题只有一个测试点,那我选择直接打表

        #include<iostream>
        using namespace std;
        const int N=1e5+10;
        const int INF=0x3f3f3f3f;
        int t,a,b;
        string s;
        int main()
        {
            cin>>t;
            while(t--)
        		cin>>a>>b>>s;
        	cout<<"62 abcdefghiz\n2 11011100000100010111110010010110011111001001100011010010001\n\n10 1234567890123456789012345678901234567890\n16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2\n\n16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2\n35 333YMHOUE8JPLT7OX6K9FYCQ8A\n\n35 333YMHOUE8JPLT7OX6K9FYCQ8A\n23 946B9AA02MI37E3D3MMJ4G7BL2F05\n\n23 946B9AA02MI37E3D3MMJ4G7BL2F05\n49 1VbDkSIMJL3JjRgAdlUfcaWj\n\n49 1VbDkSIMJL3JjRgAdlUfcaWj\n61 dl9MDSWqwHjDnToKcsWE1S\n\n\n61 dl9MDSWqwHjDnToKcsWE1S\n5 42104444441001414401221302402201233340311104212022133030\n\n5 42104444441001414401221302402201233340311104212022133030\n10 1234567890123456789012345678901234567890";
            return 0;
        }
        
        • 1

        信息

        ID
        35
        时间
        1000ms
        内存
        128MiB
        难度
        1
        标签
        递交数
        94
        已通过
        70
        上传者