193030

2018. A OK

Apr 23rd, 2020
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. void numberToLetters(int n)
  6. {
  7.     char str[11];
  8.     int i = 0;
  9.     while (n > 0) {
  10.         int remainder = n % 26;
  11.  
  12.         if (remainder == 0) {
  13.             str[i++] = 'Z';
  14.             n = (n / 26) - 1;
  15.         }
  16.         else
  17.         {
  18.             str[i++] = (remainder - 1) + 'A';
  19.             n = n / 26;
  20.         }
  21.     }
  22.     str[i] = '\0';
  23.  
  24.     reverse(str, str + strlen(str));
  25.     cout << str << endl;
  26.  
  27.     return;
  28. }
  29.  
  30. int lettersToNumber(string inputString)
  31. {
  32.  
  33.     int result = 0;
  34.     for (const auto& c : inputString)
  35.     {
  36.         result *= 26; // 0  // 26 //
  37.         result += c  - 'A' + 1; //A // A
  38.         // AAC
  39.        
  40.       //  cout << "c: " << c << endl;
  41.     }
  42.  
  43.     return result;
  44. }
  45.  
  46. int main()
  47. {
  48.     int broiTestove =0;
  49. cin >> broiTestove;
  50. cin.ignore();
  51. string input;
  52.  
  53. while(broiTestove--)
  54. {
  55.   getline(cin, input);
  56.  
  57.   char check = input.at(0);
  58.   int checkInt = check;
  59.  
  60.   if(checkInt>57)
  61.     {
  62.    //   cout << "chars"<< endl;
  63.     int result = lettersToNumber(input);
  64.     cout << result << endl;
  65.     }
  66.   else  {
  67.     int currentNum = atoi(input.c_str());
  68.     numberToLetters(currentNum);
  69.         }
  70. }
  71.  
  72. return 0;
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment