HDU-1335 Basically Speaking

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

                    Basically Speaking

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1885    Accepted Submission(s): 729

Problem Description
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
 
Source
 
Recommend
Ignatius.L
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 int cmp(int x,int y)
 4 {
 5     int s=1,i;
 6     for(i=1;i<=y;i++)
 7          s=s*x;
 8     return s;
 9 }
10 int main()
11 {
12   int t,n,s,l,i,r,j,k,p,q;
13   char str[20];
14   int a[20],b[20];
15   while(~scanf("%s",str))
16   {
17            s=j=0;
18       scanf("%d%d",&t,&n);
19         l=strlen(str);
20       for(i=0;i<l;i++)
21       {
22           if(str[i]>='0'&&str[i]<='9')
23              a[j++]=str[i]-'0';
24           else
25               a[j++]=str[i]-55;
26       }
27       k=0;
28       for(i=j-1;i>=0;i--)
29       {
30          r=a[i]*cmp(t,k); 
31            s+=r;
32             k++;
33       }
34       q=0;
35       while(s!=0)
36       {
37          p=s%n;
38          b[q++]=p;
39          s=s/n;
40       }
41       for(j=0;j<7-q;j++)
42       {
43           if(q>7)
44               break;
45               printf(" ");
46       }
47       for(i=q-1;i>=0;i--)
48       {
49           if(q>7)
50           {
51               printf("  ERROR");
52               break;
53           }
54            if(b[i]==10)
55               printf("A");
56           if(b[i]==11)
57               printf("B");
58           if(b[i]==12)
59               printf("C");
60           if(b[i]==13)
61               printf("D");
62           if(b[i]==14)
63               printf("E");
64           if(b[i]==15)
65               printf("F");
66           if(b[i]>=0&&b[i]<=9)
67               printf("%d",b[i]);
68       }
69       printf("
");
70   }
71   return 0;
72 }
73 
74 
75          
76              
77       
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88       

                    

 
 
原文地址:https://www.cnblogs.com/cancangood/p/3377910.html