Advertisement
Saleh127

UVA 10473

Sep 13th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 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. char hex[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  12. string a;
  13. ll i,j,k,l;
  14.  
  15. while(cin>>a && a[0]!='-')
  16. {
  17.  
  18. l=a.size();
  19.  
  20. if(a[1]=='x' && a[0]=='0')
  21. {
  22. ll ans=0,nm,p=0;
  23. for(i=l-1; i>1; i--)
  24. {
  25. nm=0;
  26. if(a[i]>='0' && a[i]<='9')
  27. {
  28. nm=a[i]-'0';
  29. }
  30. else
  31. {
  32. nm=a[i]-'A'+10;
  33. }
  34. ans+=(nm*pow(16,p));
  35. p++;
  36. }
  37. cout<<ans<<endl;
  38. }
  39. else
  40. {
  41. if(a=="0")
  42. {
  43. cout<<"0x0"<<endl;
  44. continue;
  45. }
  46. string ans="";
  47. ll nm=0;
  48. for(i=0; i<l; i++)
  49. {
  50. nm=(nm*10+(a[i]-'0'));
  51. }
  52. while(nm)
  53. {
  54. ans+=hex[nm%16];
  55. nm/=16;
  56. }
  57.  
  58. reverse(ans.begin(),ans.end());
  59.  
  60. cout<<"0x"<<ans<<endl;
  61. }
  62.  
  63. }
  64.  
  65.  
  66. return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement