• C++
  • 进制转换C语言解法(网上找然后自己改进了一下 有点奇葩)

  • @ 2024-9-8 19:45:23

#include<cstdio>

char values[] = {
	
	'0','1','2','3','4','5','6','7','8','9',
	'A','B','C','D','E','F','G','H','I','J',
	'K','L','M','N','O','P','Q','R','S','T',
	'U','V','W','X','Y','Z'
};

void convert_and_print(int n, int r){
	
	if(n == 0){
		
		return;
	}
		
	int x = n % r;
	
	n /= r;
	
	convert_and_print(n, r);
	
	printf("%c", values[x]);
	
	return;
}

int main() {

	int n,r;
	scanf("%d%d",&n,&r);
	
	if(n == 0){
		
		printf("%d",0);
		
		return 0;
	}
	
	convert_and_print(n, r);
	
	return 0;
}


0 comments

No comments so far...