【C语言做题系列】Basically Speaking

The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:
It will have a 7-digit display.

Its buttons will include the capital letters A through F in addition to the digits 0 through 9.

It will support bases 2 through 16.
Input
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.
Output
The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print "ERROR’’ (without the quotes) right justified in the display.
Sample Input
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15
Sample Output
120
78
1765
7CA
ERROR
11001
12D687
D071
题意:
每行输入三个数字。第一个数字将是您要转换的基数中的数字。第二个数字是你要转换的基数。第三个数字是要转换成的基数。然后根据样例输出转换过后的数。

注意:
题目有特别说明,当位数大于7的时候,要输出“ERROR”。

算法思想:
其实题目大意应该都能明白,不过操作起来可能确实有点麻烦。有很多小细节需要注意。下面放的代码会详细说明。

上代码:

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 1e5+5;
char a[maxn];          //首先我们定义了两个字符数组,一个装输入的,一个装转换后的
char b[maxn];          //有题目我们知道,输入的数字中可能包含字母,所以我们定义的是字符类型而不是整型
int change(char c)     //定义一个将字符转换成数字的函数
{
    if(c=='A')
        return 10;
    else if(c=='B')
        return 11;
    else if(c=='C')
        return 12;
    else if(c=='D')
        return 13;
    else if(c=='E')
        return 14;
    else if(c=='F')
        return 15;
    else
        return c-'0';
}
int change1(int n)    //定义一个将数字转换成字符的函数
{
    if(n==10)
        return 'A';
    else if(n==11)
        return 'B';
    else if(n==12)
        return 'C';
    else if(n==13)
        return 'D';
    else if(n==14)
        return 'E';
    else if(n==15)
        return 'F';
    else
        return n+'0';
}
int pow(int n, int len)    //此处是阶乘函数
{
    int i;
    int sum = 1;
    for(i=1; i<=len; i++)
    {
        sum*=n;
    }
    return sum;
}

int main()
{
    int basc1,basc2;           //定义两个基数
    while(scanf("%s", a)!=EOF)
    {
        scanf("%d%d", &basc1, &basc2);
        int len = strlen(a);   //找出输入字符数组a的长度,方便后续操作
        int i;
        int sum = 0;           //通过转换后的数字可以计算出当 为十进制时 的和

        for(i=0; i<len; i++)
        {
            sum+=change(a[i])*pow(basc1, len-1-i);    //此处需要好好理解
        }
        int r;
        int k = 0;
        while(sum)                    //通过不断取余得出转换后的结果
        {
            r=sum%basc2;
            b[k++]=change1(r);
            sum/=basc2;
        }
        if(k>7)                       //判断宽度是否大于7,如果大了便输出ERROR
        {
            printf("  ERROR\n");
            continue;                 //此处需要continue
        }

        for(i=1; i<=7-k; i++)          //样例输出中为右对齐,所以先要输出空格
            printf(" ");
        for(i=k-1; i>=0; i--)           //通过倒叙输出便是最终结果
            printf("%c", b[i]);
        printf("\n");
    }
}

此处再次强调几个复杂的点:
**1、
change函数里“return c -‘0’ ”返回的是int类型
change1函数里“return c +‘0’ ”返回的是char类型
2、

for(i=0; i<len; i++)
        {
            sum+=change(a[i])*pow(basc1, len-1-i);   
        }

此处将a[i]经过change函数转换后再通过阶乘计算,把总和加入到sum中
3、
我们把十进制数转换成其他进制数的时候,都是不断除以这个基数然后取余,最后倒叙一遍。所以要好好理解如下代码:

 		int r;
        int k = 0;
        while(sum)                    //通过不断取余得出转换后的结果
        {
            r=sum%basc2;
            b[k++]=change1(r);
            sum/=basc2;
        }

心得:
遇到此类问题切莫慌张,细节处理需要仔细处理,同时要记得易错点1的字符与数字的转换方法。

发布了16 篇原创文章 · 获赞 0 · 访问量 450

猜你喜欢

转载自blog.csdn.net/qq_45627679/article/details/104274745