1 solutions

  • 0
    @ 2025-3-3 16:24:08

    C :

    #include <stdio.h>
    #include <stdlib.h>
    
    #define bianWord(x) (x+55)
    #define bianNum(x) (x+'0')
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    int main(int argc, char *argv[]) {
    	int n;
    	while(scanf("%d",&n)!=EOF){
    		
    		while(n--){
    			long long a;
    		int b;
    		char c[100];
    		scanf("%lld%d",&a,&b);
    		int size=0;
    		if(a==0) {
    		printf("0\n");
    		continue;	
    		}
    		while(a>0){
    			int temp=a%b;
    			if(temp<10)
    			c[size++]=bianNum(temp);
    			else 
    			c[size++]=bianWord(temp);
    			
    			a/=b;
    		}
    		int i;
    		if(b==16) printf("0X");
    		for(i=size-1;i>=0;i--){
    			printf("%c",c[i]);
    		}
    		printf("\n");
    		}
    		
    	} 
    	return 0;
    }
    

    C++ :

    #include<iostream>
    using namespace std;
    
    void zhuan(int n,int b){
    	if (n>0){
    		zhuan(n/b,b);
    		if (n%b<10) cout<<n%b;
    		else cout<<char(n%b+'A'-10);
    	}
    }
    
    int main(){
    	int t,n,b;
    	cin>>t;
    	for (int i=1; i<=t; i++){
    		cin>>n>>b;
    		if (b==16) cout<<"0X";
    		if (n==0){
    			cout<<0<<endl;
    			continue;
    		}
    		zhuan(n,b);
    		cout<<endl;
    	}
    	return 0;
    }
    
    • 1

    Information

    ID
    10007
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By