Ading source

master
Micke Nordin 7 years ago
parent 208eb4f454
commit 128493af6b

@ -0,0 +1,52 @@
#include <iostream>
#include <cstring>
#include "lychrel.hpp"
long long get_len (string a, string b) {
return a.length() == b.length() ? a.length(): -1;
}
string add (string a, string b) {
long long len = get_len(a,b);
char result[len + 2] = { '0' };
if(len == -1) {
return "-1";
} else {
int reg = 0;
for(long long i = len +1; i >= 0; i--) {
long long rescount = i;
long long numcount = i - 1;
if(rescount == 0) {
result[rescount] = (char) reg + '0';
} else {
int x = a[numcount] - '0';
int y = b[numcount] - '0';
int temp = x+y+reg;
if(temp > 9) {
result[rescount] = (char) temp - 10 + '0';
reg = 1;
} else {
result[rescount] = (char) temp + '0';
reg = 0;
}
}
}
}
result[len+1] = '\0';
string finresult = result;
if(result[0] == 0 + '0')
finresult.erase(0,1);
return finresult;
}
bool is_palindrome (string str) {
return str == string(str.rbegin(),str.rend());
}

@ -0,0 +1,8 @@
#include <iostream>
#include <cstring>
using namespace std;
long long get_len (string a, string b);
string add (string a, string b);
bool is_palindrome (string str);

@ -0,0 +1,23 @@
#include <iostream>
#include <cstring>
#include "lychrel.hpp"
int main(int argc, char **argv)
{
string a = argv[1];
string b(a.rbegin(),a.rend());
string added = add(a,b);
long long it = 0;
do {
cout << "Iteration " << ++it << " currently working on " << added << endl;
a = added;
b = string(a.rbegin(),a.rend());
added = add(a,b);
} while(! is_palindrome(added) );
cout << "Iteration " << ++it << " currently working on " << added << endl;
cout << "That consists of\n" << a << "\n+\n" << b << "\nand that it is a palindrome." << endl;
return 0;
}
Loading…
Cancel
Save