[PAT] Class 1024 Palindromic Number (25 points)

Meaning of the questions:

Enter two positive integers N and K (N <= 1e10, k <= 100), opposite negated sum of K and N, N times the availability of a palindrome, the output number and minimum number of operations.

trick:

1e10 digital sum may reach 100 1e40, so long long will burst, the use of an array of characters operations, as well as some minor problems encountered in code comments.

Code:

HAVE_STRUCT_TIMESPEC #define
#include <bits / STDC ++ H.>
a using namespace std;
char NUM [107];
char S [107]; // Here encounter the following problems with a string of small time, access to information found directly on the string after the original length where the assignment can not increase its length, recommended s + = "xxx", in the form of string length can be increased.
K int;
int main () {
CIN >> S >> K;
int TOT = K;
int In Flag = 0;
int SIZ = strlen (S);
the while (K -) {
Memset (NUM, 0, the sizeof (NUM ));
int CNT = 0;
for (int I =-SIZ. 1; I> = 0; - I)
NUM [CNT ++] = S [I];
In Flag = 0;
for (int I =. 1; I <= CNT / 2; I ++)
IF NUM (NUM [I] = [I +-CNT. 1]) {!
In Flag =. 1;
BREAK;
}
IF (In Flag)! {
In Flag = 2;
BREAK;
}
String Y ;
int Jinwei = 0;
for (int I = CNT; I; - I) {
int TT = NUM [I] + S [-I. 1] - '0' - '0' + Jinwei;
Y [I] = 10% + TT '0 ';
Jinwei = TT / 10;
}
IF (Jinwei)
Y [0] ='. 1 ';
IF (! Y [0] = 0)
for (int I = 0; I <= CNT; I ++)
S [ I] = Y [I];
the else
for (int I =. 1; I <= CNT; I ++)
S [-I. 1] = Y [I];
IF (! S [CNT] = 0)
SIZ = CNT +1; siz used here to update the segment length s can occur when using the string s // error
the else
siz = CNT; // here if not to update the length s siz will s.size when using the string s ( ) is not updated, is still s input when the size (), not really understand, the size () - 1 position s after the assignment, can not update its size (), mistakes will occur with siz updated
}
for (int I = 0; I <SIZ; I ++)
COUT << S [I];
COUT << endl;
IF (In Flag == 2)
COUT <<-TOT-K. 1;
the else
COUT << TOT;
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11442958.html