###### ACM学习历程22——进制转换

/*
函数功能：十进制数转换成N进制数
*/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

//将十进制data转换成N进制
string TransToN(int data,int n)
{
string result="";
int index=0,i,j,temp[64];
if(data==0)
{
result="0";
}
else
{
do
{
temp[index++]=data%n;
data=data/n;
}
while(data!=0);

for(--index;index>=0;index--)
{
if(temp[index]<10)
result.append(1,temp[index]+'0');
else
result.append(1,temp[index]+'A'-10);
}
}

return result;
}

int main()
{
int data,n;
while(cin>>data>>n)
{
string result=TransToN(data,n);
cout<<"result= "<<result<<endl;
}
return 0;
}

/*
函数功能：N进制数转换成十进制数
*/
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;

//将N进制转成十进制
int  TransFromN(string data,int n)
{
int i,y,j,a[50],result=0;
int length=data.length();
for(i=0;i<length;i++)
{
if(data[i]>='0' && data[i]<='9')
a[i]=data[i]-'0';
else
a[i]=toupper(data[i])-'A'+10;
}
int power=0;
for(i=length-1;i>=0;i--)
{
y=1;
for(j=1;j<=power;j++)
y=y*n;
power++;
result+=y*a[i];
}
return result;
}

int main()
{
string data;
int n;
while(cin>>data>>n)
{
int result=TransFromN(data,n);
cout<<"result= "<<result<<endl;
}
return 0;
}

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
using namespace std;

const int N = 109;
char arr1[N];
char arr2[N];

int max(int a, int b)
{
if(a>b)
return a;
return b;
}

char con(int a)
{
if(a>=0 && a<=9) return a+'0';

switch(a)
{
case 10:
{
return 'a';
break;
}
case 11:
{
return 'b';
break;
}
case 12:
{
return 'c';
break;
}
case 13:
{
return 'd';
break;
}
case 14:
{
return 'e';
break;
}
case 15:
{
return 'f';
break;
}
case 16:
{
return 'g';
break;
}
case 17:
{
return 'h';
break;
}
case 18:
{
return 'i';
break;
}
case 19:
{
return 'j';
break;
}
}
}

int main()
{
map<char, int> mp;
mp['0'] = 0;	mp['a'] = 10;
mp['1'] = 1;	mp['b'] = 11;
mp['2'] = 2;	mp['c'] = 12;
mp['3'] = 3;	mp['d'] = 13;
mp['4'] = 4;	mp['e'] = 14;
mp['5'] = 5;	mp['f'] = 15;
mp['6'] = 6;	mp['g'] = 16;
mp['7'] = 7;	mp['h'] = 17;
mp['8'] = 8;	mp['i'] = 18;
mp['9'] = 9;	mp['j'] = 19;

while(true)
{
memset(arr1, 0, sizeof(arr1));
memset(arr2, 0, sizeof(arr2));
if(!(cin>>arr1>>arr2))
break;
int len1 = strlen(arr1);
int len2 = strlen(arr2);

reverse(arr1, arr1+len1);
reverse(arr2, arr2+len2);

int len = max(len1, len2)+5;

int temp = 0;
for(int i = 0; i<len; ++i)
{
int sum = mp[arr1[i]]+mp[arr2[i]]+temp;
temp = sum/20;
int a = sum%20;
arr1[i] = con(a);
}

i = len-1;
while(!arr1[i] || arr1[i]=='0')
{
i--;
}

for(; i>=0; --i)
{
cout<<arr1[i];
}
cout<<endl;
}
return 0;
}

#### ACM-进制转换

2014-10-03 20:45:19

#### ACM-任意的十进制数转成R进制

2013-06-16 23:11:10

#### ACM 进制转换

2014-05-09 21:37:15

#### ACM-简单题之进制转换——hdu2031

2014-03-03 21:10:11

#### 【ACM之旅】进制转换2 (任意进制转换)

2015-03-02 12:56:00

#### 杭电ACM 2031 进制转换

2012-08-03 15:35:26

#### ACM 任意进制转换

2011年03月18日 1KB 下载

#### ACM_模板_进制转换

2016-02-26 20:55:02

#### acm_十进制转二进制

2016-05-10 20:52:06

#### 用<em>c语言</em>写一个十<em>进制转换</em>成各种进制的程序

2018年04月18日 00:00

ACM学习历程22——进制转换