Skip to content

Instantly share code, notes, and snippets.

@jason790228
Last active September 27, 2017 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason790228/91209ddc3da7827334dcf5c38686ad0b to your computer and use it in GitHub Desktop.
Save jason790228/91209ddc3da7827334dcf5c38686ad0b to your computer and use it in GitHub Desktop.
10018
#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