Advertisement
Saleh127

UVA 355

Sep 18th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(0);
  8. cin.tie(0);
  9. cout.tie(0);
  10.  
  11.  
  12. ll a,c,d,e,f,i,j,k,l;
  13. char n[200];
  14. while(scanf("%lld %lld %s",&a,&c,n)==3)
  15. {
  16. f=0;
  17. ll dec=0;
  18. ll power=1;
  19. for(i=0; n[i]; i++)
  20. {
  21. dec*=a;
  22. if(n[i]>='0' && n[i]<='9')
  23. {
  24. if((n[i]-'0')>=a) f=1;
  25. else dec+=(n[i]-'0');
  26. }
  27. else
  28. {
  29. if((n[i]-'A'+10)>=a) f=1;
  30. else dec+=(n[i]-'A'+10);
  31. }
  32. }
  33. if(f)
  34. {
  35. printf("%s is an illegal base %lld number\n",n,a);
  36. }
  37. else
  38. {
  39. ll num[50]= {} ;
  40. j=-1;
  41. while(dec)
  42. {
  43. num[++j]=(dec%c);
  44. dec/=c;
  45.  
  46. }
  47. if(j<0) j=0;
  48. printf("%s base %lld = ",n,a);
  49. while(j>=0)
  50. {
  51. printf("%c", num[j] <= 9 ? num[j]+'0' : num[j]-10+'A');
  52. j--;
  53. }
  54. printf(" base %lld\n",c);
  55. }
  56.  
  57. }
  58.  
  59.  
  60. return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement