Last active
September 27, 2017 10:27
-
-
Save jason790228/91209ddc3da7827334dcf5c38686ad0b to your computer and use it in GitHub Desktop.
10018
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
using namespace std; | |
bool is_palindrome(const string & s) | |
{ | |
if (s.length() == 1) return true; | |
for (size_t i = 0; i < s.length() / 2; i++) if (s[i] != s[s.length() - 1 - i]) return false; | |
return true; | |
} | |
size_t reverse_and_add(const string& input, int& count) | |
{ | |
string input_reversed(input.rbegin(), input.rend()); | |
string result(to_string(stoul(input) + stoul(input_reversed))); | |
count++; | |
if (is_palindrome(result)) return stoul(result); | |
return reverse_and_add(result, count); | |
} | |
int main() | |
{ | |
int test_number; | |
cin >> test_number; | |
for (int i = 0; i < test_number; i++) | |
{ | |
int count(0); | |
string input; | |
cin >> input; | |
size_t result = reverse_and_add(input, count); | |
cout << count << " " << result << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment