2 条题解

  • 0
    @ 2023-12-1 23:47:50

    本题若是R为正数,那么就是标准的进制转换题。 用除模取余法容易得出答案。 模为负的情况,其实也能使用此算法,只要对C的默认%算法做下修补,使其余数为正。

    即: 除模取余法对于模为正负均适用,但规定取余时必须余数为正,这与C的取余算法不同 //修补: -1/-8=0...-1 => -1/-8=1...7

    #include <bits/stdc++.h>
    using namespace std;
    list<char> s;
    int main(){
        int n,r,d; //d: digit
        cin>>n>>r;
        cout<<n<<"=";
        while(n!=0){
            d=n%r, n/=r;
            if (d<0) d-=r,n++; //取余算法修补
            if(d>9) s.push_front(d-10+'A');
            else s.push_front(d+'0');
        }
        
        for (auto c:s) cout<<c;
        cout<<"(base"<<r<<")";
    
        return 0;
    }
    
    • 0
      @ 2022-6-12 10:30:28
      #include<iostream>
      #include<cstdio>
      #include<cmath>
      #include<cstring>
      using namespace std;
      void rec1017(int ori,int base){
      	if(ori==0)return;
      	int lef=ori%base;
      	if(lef<0)lef-=base,ori+=base;
      	if(lef>=10)lef='A'+lef-10;
      	else lef+='0';
      	rec1017(ori/base,base);
      	printf("%c",lef);//要在后面输出 
      	return;
      }
      int main(){
      	int ori,base;
      	scanf("%d%d",&ori,&base);
      	printf("%d=",ori);
      	rec1017(ori,base);
      	printf("(base%d)",base);
      	return 0;
      }//30000=11011010101110000(base-2)
      
      • 1

      信息

      ID
      18
      时间
      1000ms
      内存
      125MiB
      难度
      2
      标签
      递交数
      18
      已通过
      15
      上传者