1 条题解

  • 0
    @ 2021-6-14 23:29:14

    C :

    #include<stdio.h>
    #include<string.h>
    char base[21]="0123456789ABCDEFGHIJ";
    int main(){
    	int N,b,r,i;
    	char ans[30]={0};
    	while(scanf("%d%d",&N,&b)==2){
    		printf("%d=",N);
    		i=0;
    		while(N!=0){
    			r=N%b;
    			N=N/b;
    			if(r<0){
    				r-=b;
    				N++;
    			}
    			ans[i++]=base[r];
    		}
    		for(i=strlen(ans)-1;i>=0;i--){
    			printf("%c",ans[i]);
    		}
    		printf("(base%d)\n",b);
    	}
    	return 0;
    }
    

    C++ :

    #include <iostream>
    #include <string>
    using namespace std;
    const char nc[20] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
    string ans;
    int main()
    {
    	int m, n, k, t, s;
    	for(;cin >> m >> n;)
    	{
    		ans = "";
    		s = m;
    		for(;m != 0;)
    		{
    			k = m % n;
    			t = m / n;
    			if(k < 0)
    			{
    				k -= n;
    				t++;
    			}
    			m = t;
    			ans.push_back(nc[k]);
    		}
    		cout << s << "=";
    		int i = ans.length() - 1;
    		while(i >= 0)
    		{
    			cout << ans[i];
    			i--;
    		}
    		cout << "(base" << n << ")" << endl;
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    203
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者