您的位置:首页 > 其它

【进制转换】十进制转其他进制 _CDTemplate

2015-04-25 16:20 225 查看
CodeHunt里有个进制转换的36进制转换,以前在清澄做过,但是可惜没放在CSDN过,这个着实是暴力的进制转换没有错,但是为了以后的速度,还是在这里存一份好了……

顺便提一句,特别地,对十六进制的转换时:/article/1566928.html

对于任何一个数字,及10-36间的进制基数,接口 TBase(n,m)

【n】 long long 长度的原十进制整数

【m】 进制基数Base

Code:

#include <cmath> 
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))

bool cmp(const int a, const int b)
{
	return a > b;
}

string TBase(ll in,int m)
{
	string s="";
	ll n=in;
	while(n)
	{
		ll mod=n%m;
		if(mod) s+= (mod<10)? '0'+mod : 'A'+mod-1 ;
		n/=m;
		if(!n) break;
	}
	return s;
}

int main()
{
	ll n;	int m;
	cin>>n>>m;
	cout<<"Trans "<< n <<" to Base-"<< m <<" :"<<TBase(n,m); 
	return 0;
}


几年前清澄上吾辈的C语言代码,怀念~ ~ 贴一下:

#include<stdio.h>

#include<string.h>

int str[10000];

int main()

{

int n,m,len=1;

scanf("%d %d",&n,&m);

if(!n) {

printf("0\n");

return 0;}

while(n)

{

int mod=n%m;

str[len++]=mod;

n=(n-mod)/m;

if(!n) break;

}

while(--len)

{

if(str[len]<10) printf("%d",str[len]);

else printf("%c",str[len]-10+'A');

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: