每日模板一练——进制转换

输入两个整数n,k,第一个为十进制下的数,将n转化为k进制。

#include<bits/stdc++.h>
using namespace std;
const int MAXN=51;

int n,k;
int cnt;
char num[MAXN];
int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

int main(){
	n=Read(),k=Read();
	if(n<0)
	  cout<<"-",n=-n;
	while(n){
		cnt++;
		int ys=n%k;
		if(ys<10)
		  num[cnt]=ys+'0';
		else
		  num[cnt]='A'+ys-10;
		n/=k;
	}
	for(int i=cnt;i>=1;--i)
	  cout<<num[i];
	return 0;
}

 

posted @ 2018-10-30 18:46  Ishtar~  阅读(141)  评论(0编辑  收藏  举报