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

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

顺便提一句,特别地,对十六进制的转换时:http://blog.csdn.net/okcd00/article/details/45196255 

对于任何一个数字,及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语言代码,怀念~ ~ 贴一下:

  1. #include<stdio.h>
  2. #include<string.h>
  3. int str[10000];
  4. int main()
  5. {
  6.     int n,m,len=1;
  7.     scanf(“%d %d”,&n,&m);
  8.     if(!n) {
  9.     printf(“0\n”);
  10.     return 0;}
  11.     while(n)
  12.     {
  13.         int mod=n%m;
  14.         str[len++]=mod;
  15.         n=(n-mod)/m;
  16.         if(!n) break;
  17.     }
  18.     while(–len)
  19.     {
  20.         if(str[len]<10) printf(“%d”,str[len]);
  21.         else printf(“%c”,str[len]-10+‘A’);
  22.     }
  23. }

    原文作者:进制转换
    原文地址: https://blog.csdn.net/okcd00/article/details/45271067
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞