#P7379. Restore Missing Digit in Bank Card Number Using Checksum Algorithm
Restore Missing Digit in Bank Card Number Using Checksum Algorithm
Restore Missing Digit in Bank Card Number Using Checksum Algorithm
Given a bank card number with exactly one missing digit (represented by a question mark '?' in the input), your task is to determine the missing digit such that the card becomes valid under the checksum algorithm described below.
The checksum algorithm is defined as follows:
- Starting from the second-to-last digit and moving left, every alternate digit is multiplied by . The remaining digits are left unchanged.
- For each digit that was multiplied by , calculate the sum of its digits (i.e. if the product is greater than or equal to , subtract from it).
- Sum all the resulting digits from step 1 and 2 (excluding the check digit, which is the last digit). Then multiply the sum by and take the result modulo .
- The computed value from step 3 must equal the check digit (i.e. the last digit of the card number).
For example, to validate the card number , the computation is organized as follows:
[ \begin{array}{|c|c|c|c|c|c|c|c|c|c|c|} \hline \text{Card Digits} & 7 & 9 & 9 & 2 & 7 & 3 & 9 & 8 & 7 & 1 & 3 \ \hline \text{Multiply Every Other Digit} & 7 & \red{18} & 9 & \red{4} & 7 & \red{6} & 9 & \red{16} & 7 & \red{2} & - \ \hline \text{After Summing Digits} & 7 & \green{9} & 9 & 4 & 7 & 6 & 9 & \green{7} & 7 & 2 & =67 \ \hline \end{array} ] [ 67 \times 9 \bmod 10 = 3 ] Since the result equals the last digit (), the card number is valid.
When one digit is missing from the input card number, fill in the appropriate digit so that the complete card number satisfies the checksum condition described above.
inputFormat
Input is a single string representing the bank card number with exactly one missing digit. The missing digit is denoted by a question mark '?' in the string. For instance, a valid input could be: 7992739871?
outputFormat
Output the full valid bank card number after replacing the '?' with the correct digit so that the checksum algorithm holds.
sample
7992739871?
79927398713