|
|
back to boardWA#4 Don`t know what to do. My prog passes all tests like 1992, 199999992, 1-digit numbers. It passes all teste I`ve seen on this forum. Please, tell me, what can be the problem. Thank you. #include <stdio.h> #include <conio.h> #include <iostream> #include <string> using namespace std; typedef long long ll; string make_palindrome(string str) { string palindrome = str; if (str.size() % 2 == 0) { int j = 0; for (int i = 0; i < str.size() / 2; i++,j++) palindrome[j] = str[i]; for (int i = str.size() / 2 - 1; i >= 0; i--,j++) palindrome[j] = str[i]; } else { int j = 0; for (int i = 0; i < str.size() / 2; i++,j++) palindrome[j] = str[i]; for (int i = str.size() / 2; i >= 0; i--,j++) palindrome[j] = str[i]; } return palindrome; } void add_1(string *str) { int start = 0; if ((str->size() - 1) % 2 == 0) start = (str->size() - 1) / 2; else start = (str->size() - 1) / 2 + 1; for (int i = start; i >= 0; i--) { if ((*str)[i] + 1 < '9') { if ( i == start)(*str)[i]++; break; } else { (*str)[i] = '0'; (*str)[i - 1]++; } } } void extract_1(string *str) { int start = 0; if ((str->size() - 1) % 2 == 0) start = (str->size() - 1) / 2; else start = (str->size() - 1) / 2 + 1; for (int i = start; i >= 0; i--) { if ((*str)[i] - 1 >= '0') { (*str)[i]--; break; } else { (*str)[i] = '9'; } } } int main(void) { string in; cin >> in; string stemp = make_palindrome(in); string ans; if (in.size() == 1) { cout << in; return 0; } if (in.compare(stemp) == 1) { string str = '0' + in; add_1(&str); if (str[0] != '0') ans = make_palindrome(str); else { string temp = str; temp.erase(temp.begin()); ans = make_palindrome(temp); } } else if (in.compare(stemp) == -1) { string str = '0' + in; extract_1(&str); if (str[0] != '0') ans = make_palindrome(str); else { string temp = str; temp.erase(temp.begin()); ans = make_palindrome(temp); } } else { cout << in; return 0; }
if (ans.compare(stemp) == 1) { cout << ans; } else if (ans.compare(stemp) == -1) { cout << stemp; } else cout << stemp; return 0; } Edited by author 20.10.2017 17:12 Re: WA#4 Input: 798. Output: 1001. Problem in add_1 function, changed < '9' on <= '9' got AC. |
|
|