[备战NOI同步赛]适合ACM-ICPC使用的进制转换模板

进制转换不必多说,反向取余与累加法,下面是模板代码

/*
进制转换模板
By:qpswwww(ZYK)
调用接口:Hugeint.scan()
*/
#define MAXN 1000 //数字长度
#define SYS 10

int M=2; //M为转换的进制数

char ch[]={"0123456789ABCDEF"};

int nToTen(Hugeint in) //n进制数in转十进制
{
	int i,j,tot=0;
	for(i=in.size;i>=1;i--)
	{
		tot*=M;
		tot+=in.s[i];
	}
	return tot;
}
Hugeint TenTon(int in) //十进制数in转n进制
{
	Hugeint out;
	out.size=0;
	out.sign=1;
	while(in>0)
	{
		out.size++;
		out.s[out.size]=in%M;
		in/=M;
	}
	return out;
}
void scanN(Hugeint &out) //输入n进制数out
{
	memset(out.s,0,sizeof(out.s));
	int i=0,j;
	char in[MAXN];
	scanf("%s",in);
	if(in[0]=='-') out.sign=-1,i++;
	else if(in[0]=='0') out.sign=0;
	else out.sign=1;
	out.size=strlen(in)-i;
	for(j=out.size;j>0;i++,j--)
	{
		if(in[i]>='A') out.s[j]=in[i]-'A'+10;
		else out.s[j]=in[i]-'0';
	}
}
void printN(Hugeint in) //输出n进制数in
{
	int i;
	if(in.sign==-1) printf("-");
	else if(in.sign==0)
	{
		printf("0");
		return;
	}
	for(i=in.size;i>=1;i--)
	{
		printf("%c",ch[in.s[i]]);
	}
}



 

例题(Wikioi 1474):

将十进制数n转换成m进制数 m<=16

n<=100

共一行

n和m

共一个数

表示n的m进制

样例1:10 2

样例2:100 15

样例1:1010

样例2:6A

 

代码

/*
进制转换模板
By:qpswwww(ZYK)
调用接口:Hugeint.scan()
*/
#include <stdio.h>
#include <string.h>

#define MAXN 1000 //数字长度
#define SYS 10

int M=2; //M为转换的进制数

char ch[]={"0123456789ABCDEF"};

struct Hugeint //高精度
{
	int s[MAXN];
	int size; //数字大小
	int sign; //符号,1为正,-1为负,0为零
	Hugeint operator + (Hugeint b) //高精度加法
	{
		int i,j;
		Hugeint out;
		out.s[0]=0;
		out.size=size;
		if(b.size>size) out.size=b.size;
		for(i=1;i<=out.size;i++) out.s[i]=s[i];
		for(i=1;i<=out.size;i++)
			out.s[i]+=b.s[i];
		for(i=1;i<=size;i++)
		{
			if(out.s[i-1]/SYS>0)
			out.s[i]+=out.s[i-1]/SYS;
			out.s[i-1]%=SYS;
		}
		if(out.s[size]/SYS>0)
		{
			out.size++;
			out.s[size]=out.s[size-1]/SYS;
			out.s[size-1]%=SYS;
		}
		return out;
	}
	Hugeint operator*(Hugeint b) //高精度乘法
	{
		int i,j;
		Hugeint out;
		memset(out.s,0,sizeof(out.s));
		out.sign=sign*b.sign;
		for(i=1;i<=size;i++)
		{
			for(j=1;j<=b.size;j++)
			{
				out.s[i+j-1]+=(s[i]*b.s[j]);
			}
		}
		for(i=1;i<MAXN;i++)
		{
			if(out.s[i-1]/SYS>0)
			out.s[i]+=out.s[i-1]/SYS;
			out.s[i-1]%=SYS;
		}
		for(i=MAXN-1;i>=1;i--)
			if(out.s[i]) break;
		out.size=i;
		return out;
	}
	bool operator>(Hugeint b) //高精度大于
	{
		int i,j;
		if(sign==b.sign&&sign==-1)
		{
			if(size<b.size) return 1;
			for(i=size;i>=1;i--)
			{
				if(s[i]>b.s[i]) return 0;
				if(s[i]<b.s[i]) return 1;
			}
		}
		else
		{
			if(sign>b.sign) return 1;
			if(size>b.size) return 1;
			for(i=size;i>=1;i--)
			{
				if(s[i]>b.s[i]) return 1;
				if(s[i]<b.s[i]) return 0;
			}
		}
		return 0;
	}
	bool operator<(Hugeint b) //高精度小于
	{
		int i,j;
		if(sign==b.sign&&sign==-1)
		{
			if(size<b.size) return 0;
			for(i=size;i>=1;i--)
			{
				if(s[i]>b.s[i]) return 1;
				if(s[i]<b.s[i]) return 0;
			}
		}
		else
		{
			if(sign>b.sign) return 0;
			if(size>b.size) return 0;
			for(i=size;i>=1;i--)
			{
				if(s[i]>b.s[i]) return 0;
				if(s[i]<b.s[i]) return 1;
			}
		}
		return 0;
	}
	void print() //输出
	{
		int i,flag=0;
		if(sign==-1) printf("-");
		else if(sign==0)
		{
			printf("0");
			return;
		}
		for(i=size;i>=1;i--)
		{
			if(flag) printf("%d",s[i]);
			else if(s[i]) printf("%d",s[i]),flag=1;
		}
	}
	void scan() //输入
	{
		memset(s,0,sizeof(s));
		int i=0,j;
		char in[MAXN];
		scanf("%s",in);
		if(in[0]=='-') sign=-1,i++;
		else if(in[0]=='0') sign=0;
		else sign=1;
		size=strlen(in)-i;
		for(j=size;j>0;i++,j--)
			s[j]=in[i]-'0';
	}
};

int nToTen(Hugeint in) //n进制数in转十进制
{
	int i,j,tot=0;
	for(i=in.size;i>=1;i--)
	{
		tot*=M;
		tot+=in.s[i];
	}
	return tot;
}
Hugeint TenTon(int in) //十进制数in转n进制
{
	Hugeint out;
	out.size=0;
	out.sign=1;
	while(in>0)
	{
		out.size++;
		out.s[out.size]=in%M;
		in/=M;
	}
	return out;
}
void scanN(Hugeint &out) //输入n进制数out
{
	memset(out.s,0,sizeof(out.s));
	int i=0,j;
	char in[MAXN];
	scanf("%s",in);
	if(in[0]=='-') out.sign=-1,i++;
	else if(in[0]=='0') out.sign=0;
	else out.sign=1;
	out.size=strlen(in)-i;
	for(j=out.size;j>0;i++,j--)
	{
		if(in[i]>='A') out.s[j]=in[i]-'A'+10;
		else out.s[j]=in[i]-'0';
	}
}
void printN(Hugeint in) //输出n进制数in
{
	int i;
	if(in.sign==-1) printf("-");
	else if(in.sign==0)
	{
		printf("0");
		return;
	}
	for(i=in.size;i>=1;i--)
	{
		printf("%c",ch[in.s[i]]);
	}
}
int main()
{
	int n;
	Hugeint out;
	scanf("%d%d",&n,&M);
	out=TenTon(n);
	printN(out);
	printf("\n");
	return 0;
}



 

你可能感兴趣的:([备战NOI同步赛]适合ACM-ICPC使用的进制转换模板)