Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// https://trello.com/c/R1Trslg7/70-uva-10018-reverse-and-add
- /// https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=959
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- string suma (string a, string b)
- {
- long may = max( a.size(), b.size() );
- reverse( a.begin(), a.end() );
- reverse( b.begin(), b.end() );
- for(long i=a.size(); i<may; i++) ///que me haga max-x.size() de operaciones
- a.push_back('0');
- for(long i=b.size(); i<may; i++) ///que me haga max-x.size() de operaciones
- b.push_back('0');
- bool ac = false;
- string c;
- for(long i=0; i<a.size(); i++)
- {
- long A = a[i]-'0';
- long B = b[i]-'0';
- long C = A+B+ac;
- ac = C/10;
- c.push_back( (C)%10 +'0' );
- }
- if(ac)
- c.push_back('1');
- reverse(c.begin(), c.end());
- return c;
- }
- bool es_palindromo(string s)
- {
- bool palindromo = true;
- for(long i=0; i<s.size() && palindromo; i++)
- if(s[i]!=s[s.size()-i-1])
- palindromo = false;
- return palindromo;
- }
- void reverse_and_add(string s)
- {
- long c=0;
- while( !es_palindromo(s) )
- {
- c++;
- string aux = s;
- reverse( aux.begin(), aux.end() );
- s = suma(s,aux);
- }
- cout<<c<<" "<<s<<endl;
- }
- int main()
- {
- long n;
- string s;
- cin>>n;
- for (long i=0; i<n; i++)
- {
- cin>>s;
- reverse_and_add(s);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment