Advertisement
wojiaocbj

Untitled

Mar 13th, 2023
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. /*
  2.  Author: 曹北健(37509)
  3.  Result: AC Submission_id: 5207186
  4.  Created at: Tue Mar 14 2023 10:15:51 GMT+0800 (China Standard Time)
  5.  Problem: 6695  Time: 5 Memory: 1508
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12. #include <math.h>
  13. int main(){
  14. #ifdef LINUX_DEBUG
  15.     freopen("../input.txt", "r", stdin);
  16. #endif
  17.     int a[64] = { 0 }, T;
  18.  
  19.     char input[128] = { 0 };
  20.     scanf("%d", &T);
  21.     while(T--){
  22.         long long base1, base2, s = 0, s2, tmp1;
  23.         long long cur = 1;
  24.         scanf("%lld%lld%s", &base1, &base2, input);
  25.         //input no '-'
  26.         int n = strlen(input), i;
  27.         if(n == 1 && input[0] == '0'){
  28.             puts("0");
  29.             continue;
  30.         }
  31.         for(i = n - 1;i >= 0;i--){
  32.             if(isupper(input[i])){
  33.                 s += (input[i] - 'A' + 10) * cur;
  34.             }
  35.             else{//digit
  36.                 s += (input[i] - '0') * cur;
  37.             }
  38.             cur *= base1;
  39.         }
  40.         n = 0;
  41.         //printf("%lld\n", s);
  42.         s2 = s;
  43.         while(s2){
  44.             tmp1 = s2 % base2;
  45.             s2 /= base2;
  46.             if(tmp1 < 0){//base2<0
  47.                 s2 += 1;tmp1 -= base2;
  48.             }
  49.             a[n] = tmp1;
  50.             n++;
  51.         }
  52.         while(n--){
  53.             if(a[n] >= 10){
  54.                 putchar(a[n] - 10 + 'A');
  55.             }
  56.             else{
  57.                 putchar('0' + a[n]);
  58.             }
  59.         }
  60.         putchar('\n');
  61.     }
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement