首页 > ACM题库 > HDU-杭电 > HDU 1335 Basically Speaking-模拟-[解题报告] C++
2013
12-09

HDU 1335 Basically Speaking-模拟-[解题报告] 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.

输入:

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.

输出:

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.

样例输入:

1111000  2 10
1111000  2 16
2102101  3 10
2102101  3 15
  12312  4  2
     1A 15  2
1234567 10 16
   ABCD 16 15

样例输出:

    120
     78
   1765
    7CA
  ERROR
  11001
 12D687
   D071

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1335

题意:前面是一个7位a进制数,表示成后面的进制。如果超过7位则输出ERROR。直接模拟。

代码:

# include <stdio.h>


char str[40] ;


int chartonum(char ch)
{
    if (ch >= '0' && ch <= '9') return ch-'0' ;
    return ch-'A'+10 ;
}


int gao(char str[], int b)
{
    int i, rtn = 0 ;
    for (i = 0 ; str[i] ; i++)
    {
        if (chartonum(str[i])>=b) return -1 ;
        rtn = rtn*b + chartonum(str[i]) ;
    }
    return rtn ;
}


void gao2(int num, int b)
{
    int i, cnt = 0 ;
    char s[40], tab[] = "0123456789ABCDEF" ;
    while (num)
    {
        s[cnt++] = tab[num%b] ;
        num /= b ;
    }
    if (cnt > 7){
        printf ("%7s\n", "ERROR") ;
        return ;
    }
    for (i = 0 ; i < cnt ; i++)
        str[cnt-i-1] = s[i] ;
    str[cnt] = '\0' ;
    printf ("%7s\n", str) ;
}


int main ()
{
    int num, b1, b2 ;
    while (~scanf ("%s  %d %d", &str, &b1, &b2))
    {
        num = gao(str, b1) ;
        if (num == -1) printf ("%7s\n", "ERROR") ;
        gao2(num, b2) ;
    }
    return 0 ;
}

解题报告转自:http://www.cnblogs.com/lzsz1212/archive/2012/02/16/2353534.html


  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  2. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }