codeup- number conversion

1942: number conversion

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 2738  Solved: 693
[ Submit][ Status][ Web Board][Creator:Imported]

Description

Find any two different non-binary conversion negative integer (binary hex to 16), it can be an integer in the range of a given expression in the long.
Different hexadecimal notation is (0,1, ..., 9, a , b, ..., f) , or (0,1, ..., 9, A , B, ..., F) .

Input

Only one line input, comprising three integers a, n, b. a represents a subsequent ary n is an integer, b represents a hexadecimal integer n wishing to be converted into binary integer b. a, b is a decimal integer, 2 = <a, b <= 16.

Output

May be multiple sets of test data for each set of data, the output contains a line, the line has a binary integer b is converted. When output symbols for all uppercase letters represented, namely (0,1, ..., 9, A, B, ..., F).

Sample Input

4 123 10

Sample Output

27

HINT

String represents a different storage and hexadecimal.

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 int main(){
  5     int a, b;
  6     char s[40];
  7     while(scanf("%d %s %d", &a, s, &b) != EOF){
  8         int len = strlen(s);
  9         //将字符串转换成数值!!! 
 10         for(int i=0; i<len; i++){
 11             if(s[i]>='0' && s[i]<='9') s[i] = s[i] - '0';
 12             if(s[i]>='a' && s[i]<='f') s[i] = s[i] - 'a' + 10;
 13             if(s[i]>='A' && s[i]<='F') s[i] = s[i] - 'A' + 10;
 14         }
 15         int sum=0, product=1;
 16         for(int i=len-1; i>=0; i--) {
 17             sum = sum + (int)s[i] * product;
 18             product = product * a;
 19         }
 20         int num = 0;
 21         //初始化!!! 
 22         memset(s, 0, sizeof(s));
 23         if(b==10){
 24             printf("%d\n", sum);
 25         }the else {
 26              // wonderful mode of transformation! ! ! 
27              do {
 28                  IF (SUM% B < 10 ) {
 29                      S [NUM ++] = SUM% B + ' 0 ' ;
 30                  } the else {
 31 is                      S [NUM ++] = SUM% B - 10 + ' A ' ;
 32                  }
 33 is                  SUM / = B;
 34 is              } the while (SUM =! 0 );
 35              for (int i=num-1; i>=0; i--){
 36                 printf("%c", s[i]);
 37             }
 38             printf("\n");
 39         }
 40     }
 41     return 0;
 42 }
 43 
 44 /*
 45 //自己写的 
 46 void outPrintf(int n){
 47     if(n<10){
 48         printf("%d", n);
 49     }else{
 50         switch(n){
 51             case 10:
 52                 printf("A");
 53                 break;
 54             case 11:
 55                 printf("B");
 56                 break;
 57             case 12:
 58                 printf("C");
 59                 break;
 60             case 13:
 61                 printf("D");
 62                 break;
 63             case 14:
 64                 printf("E");
 65                 break;
 66             case 15:
 67                 printf("F");
 68                 break;
 69         }
 70     }
 71 }
 72 
 73 int changeNum(char c){
 74     int num;
 75     if(c=='0'){
 76         num = 0;
 77     }else if(c=='1'){
 78         num = 1;
 79     }else if(c=='2'){
 80         num = 2;
 81     }else if(c=='3'){
 82         num = 3;
 83     }else if(c=='4'){
 84         num = 4;
 85     }else if(c=='5'){
 86         num = 5;
 87     }else if(c=='6'){
 88         num = 6;
 89     }else if(c=='7'){
 90         num = 7;
 91     }else if(c=='8'){
 92         num = 8;
 93     }else if(c=='9'){
 94         num = 9;
 95     }else if(c=='a' || c=='A'){
 96         num = 10;
 97     }else if(c=='b' || c=='B'){
 98         num = 11;
 99     }else if(c=='c' || c=='C'){
100         num = 12;
101     }else if(c=='d' || c=='D'){
102         num = 13;
103     }else if(c=='e' || c=='E'){
104         num = 14;
105     }else{
106         num = 15;
107     }
108     return num;
109 }
110 
111 void getNum(char s[], int len, int ans[]){
112     int num;
113     for(int i=0, j=len-1; i<len; i++, j--){
114         ans[i] = changeNum(s[j]);
115     }
116 }
117 
118 int main(){
119     int a, b;
120     char s [40]; // n is not necessarily the book number between 0-9, may appear a, A and the like! It is necessary with a string storage 
 121      the while (! Scanf ( "% D% S% D", & A, S, & B) = the EOF) {
 122          int ANS [40], NUM;
 123          int len = strlen (S);
 124          getNum (S, len, ANS);
 125          unsigned Long Long int SUM = 0, Product =. 1;
 126          for (int I = 0; I <len; I ++) {
 127              SUM = SUM + ANS [I] * Product;
 128              * A = Product Product;
 129          }
 130.          NUM = 0;
 131 is          do {
 132              ANS [NUM ++] = SUM% B;
 133              SUM / = B;
 134         }while(sum!=0);
135         for(int i=num-1; i>=0; i--){
136             outPrintf(ans[i]);
137         }
138         printf("\n");
139     }
140     return 0;
141 }
142 */

 

Guess you like

Origin www.cnblogs.com/heyour/p/12149879.html