自己编的进制转换函数= =
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int N = 20000;
const int INF = 1000000;
char a[N];
int flag;
void conv(int x, int y)
{
memset(a, 0, sizeof(a));
int res, i = 0;
while(x != 0)
{
res = x % y;
x = x / y;
if(res < 10) a[i ++] = '0' + res;
else a[i ++] = 'A' + (res - 10);
}
strrev(a);
}
int main()
{
// freopen("in.txt", "r", stdin);
int n, r;
while(~scanf("%d%d", &n, &r))
{
flag = 1;
if(n < 0)
{
flag = 0;
n *= -1;
}
conv(n, r);
if(flag) printf("%s\n", a);
else printf("-%s\n", a);
}
return 0;
}
进制转换的应用= =
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int N = 20000;
const int INF = 1000000;
int conv(int x, int y)
{
int res, sum = 0;
while(x != 0)
{
res = x % y;
x = x / y;
sum += res;
}
return sum;
}
int main()
{
// freopen("in.txt", "r", stdin);
int n;
while(~scanf("%d", &n) && n)
{
if(conv(n, 10) == conv(n, 16) && conv(n, 16) == conv(n, 12)) printf("%d is a Sky Number.\n", n);
else printf("%d is not a Sky Number.\n", n);
}
return 0;
}
任意进制转换,就是把它先转化为十进制,再调用2031的函数。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <ctype.h>
#include <string.h>
using namespace std;
const int N = 20000;
const int INF = 1000000;
char a[N];
int flag;
int conv1(char b[N], int x)
{
int sum = 0, i;
int len = strlen(b);
for(i = 0; i < len; i ++)
{
if(isdigit(b[i])) sum += (b[i] - '0') * pow(x, (len - i - 1));
else sum += (b[i] - 'A' + 10) * pow(x, (len - i - 1));
}
return sum;
}
void conv2(int x, int y)
{
memset(a, 0, sizeof(a));
int res, i = 0;
while(x != 0)
{
res = x % y;
x = x / y;
if(res < 10) a[i ++] = '0' + res;
else a[i ++] = 'A' + (res - 10);
}
strrev(a);
}
int main()
{
// freopen("in.txt", "r", stdin);
int num1, num2;
char s[N];
while(~scanf("%s", s))
{
cin >> num1;
cin >> num2;
int n = conv1(s, num1);
conv2(n, num2);
int len = strlen(a);
if(len > 7) printf(" ERROR\n");
else printf("%7s\n", a);
}
return 0;
}