hdu2031 进制转换 && hdu2097 Sky数 && hdu1335 Basically Speaking(进制转换)

自己编的进制转换函数= =

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值