UVA355 UVALive5249 The Bases Are Loaded【进制】

Write a program to convert a whole number specified in any base (2..16) to a whole number in any other base (2..16). “Digits” above 9 are represented by single capital letters; e.g. 10 by A, 15 by F, etc.

Input

Each input line will consist of three values. The first value will be a positive integer indicating the base of the number. The second value is a positive integer indicating the base we wish to convert to. The third value is the actual number (in the first base) that we wish to convert. This number will have letters representing any digits higher than 9 and may contain invalid “digits”. It will not exceed 10 characters. Each of the input values on a single line will be separated by at least one space.

Output

Program output consists of the original number followed by the string ‘base’, followed by the original base number, followed by the string ‘=’ followed by the converted number followed by the string ‘base’followed by the new base. If the original number is invalid, output the statement

original_V alue is an illegal base original_Base number

where original_V alue is replaced by the value to be converted and original_Base is replaced by the original base value.

Sample input

2 10 10101

5 3 126

15 11 A4C

Sample output

10101 base 2 = 21 base 10

126 is an illegal base 5 number

A4C base 15 = 1821 base 11


Regionals 1990 >> North America - Southeast USA


问题链接UVA355 UVALive5249 The Bases Are Loaded

问题简述:(略)

问题分析

  进制转换问题,不解释。有关解释参见参考链接。

程序说明:(略)

题记

  把功能封装到函数中是个好主意。


参考链接POJ NOI0113-01 数制转换(Bailian2710)【进制】


AC的C++语言程序如下:

/* UVA355 UVALive5249 The Bases Are Loaded */

#include <bits/stdc++.h>

using namespace std;

#define N 72
char s[N], ans[N];

void convert(int base1, char s[], int base2)
{
    long val, dcount, digit;
    int i, j;

    val = 0;
    for(i=0; s[i]; i++) {
        if(isdigit(s[i]))
            val = val * base1 + s[i] - '0';
        else
            val = val * base1 + toupper(s[i]) - 'A' + 10;
    }

    dcount = 0;
    while(val) {
        digit = val % base2;
        val /= base2;

        ans[dcount++] = ((digit >= 10) ? 'A' - 10 : '0') + digit;
    }
    if(dcount == 0) {
        ans[dcount++] = '0';
        ans[dcount] = '\0';
    } else
        ans[dcount] = '\0';

    // reverse
    for(i=0, j=dcount-1; i<j; i++, j--) {
        char c = ans[i];
        ans[i] = ans[j];
        ans[j] = c;
    }
}

bool check(int base, char s[])
{
        for(int i=0; s[i]; i++) {
            int d = isdigit(s[i]) ? s[i] - '0' : s[i] - 'A' + 10;

            if(d >= base)
                return false;
        }
        return true;
}

int main()
{
    int b1, b2;

    while(~scanf("%d%d%s", &b1, &b2, s)) {
        if(check(b1, s)) {
            convert(b1, s, b2);

            printf("%s base %d = %s base %d\n", s, b1, ans, b2);;
        } else
            printf("%s is an illegal base %d number\n", s, b1);
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值