2013
12-09

# 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

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

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