PAT B1031 check ID
Subject description:
A valid area 17 by the ID number, order number, and date code plus a checksum component. Checksum calculation rule is as follows:
First, a weight is assigned to the first 17-bit digital weighted sum, right: {7,9,10,5,8,4,2,1,6,3,7,9,10,5 , 8,4,2}; and then the calculated value of 11 obtained modulo Z; Z value corresponding to the final value of the check code M in accordance with the following relationship:
Z: 0. 5. 4. 3. 1 2. 6. 7. 9 10. 8
M : 1 0 X 9 8 7 6 5 4 3 2
are now given number identification number, please verify the validity of check code, and outputs the number in question.
Input format:
input of the first row is given a positive integer N (≤100) is the number of the input ID number. Then N rows, each row 18 is given an ID number.
Output format:
each line of output an ID number in question in the order of input. Here is not whether a reasonable inspection before 17, just before checking whether all numbers 17 and finally a checksum is calculated accurately. If all the numbers are normal, the output All passed.
Input Sample. 1:
. 4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
sample output. 1:
12010X198901011234
110108196711301866
37070419881216001X
Input Sample 2:
2
320124198808240056
110108196711301862
output sample Example 2 :
All passed
Reference Code:
1 / * *********************************************** **** 2 PAT B1031 check ID 3 *************************************** ************ * / . 4 #include <the iostream> . 5 #include < String > . 6 . 7 the using namespace STD; . 8 . 9 const int the WEIGHT [ . 17 ] = { . 7 , . 9 , 10 , . 5 , 8 , 4 , 2 , 1 , 6 , 3 ,7, 9, 10, 5, 8, 4, 2 }; 10 const char CHECK_SYM[11] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3' , ' 2 ' }; . 11 12 is int main () { 13 is int COUNT = 0 , illegalCount = 0 ; 14 15 CIN >> COUNT; 16 . 17 String IDNUM; 18 is int I = 0 , J = 0 ; . 19 for (I = 0 ; I <COUNT; ++ I) { 20 is CIN >> IDNUM; // read ID No. 21 is 22 is int the checkNum = 0 ; // first 17 digits and the corresponding weight and the sum of products 23 // before checking whether the ID number 17 has a non-numeric 24 for (J = 0 ; J < . 17 ; ++ J) { 25 IF (IDNUM [J] - ' 0 ' < 0 || IDNUM [J] - ' 0 ' > . 9 ) BREAK ; 26 is 27 the checkNum + = (IDNUM [J] - ' 0 ' ) * the WEIGHT [J]; 28 } 29 30 // If the previous 17 there are non-numeric digit or check digit is incorrect, the output error ID number and update information 31 IF (J! =. 17 || IDNUM [ . 17 !] = CHECK_SYM [the checkNum% . 11 ]) { 32 IF (illegalCount =! 0 ) COUT << endl; // if 0 is not the first error, and the need to output a line feed error identification number partition 33 is 34 is COUT << IDNUM; 35 illegalCount ++; // number of erroneous identification number is increased 36 } 37 [ } 38 is 39 IF (illegalCount == 0 ) << COUT " All passed " ; // no error identification number case 40 41 is return 0; 42 }
Precautions:
1: About the "final can not have extra spaces / line output after a" subject requirements, there are two ways to respond:
1.1: output number is known, can be output to determine whether space or line break has been to determine whether the last output:
1 // output known number 2 for ( int I = 0 ; I <ans.size (); ++ I) { . 3 COUT << ANS [I]; . 4 . 5 IF ! (I = ans.size () - . 1 ) COUT << endl; . 6 }
1.2: output number is unknown, may be added for assisting in determining an integer: when the item to be output is determined to meet the requirements of integer output values are not initialized, if it is stated that this is the first output, the output does not need to change the front row ; otherwise explained before the current output has an output, while the output is not behind this new line, we need to output a newline before the output of the current output. Whether the current behind this new line is output is determined by the next output if no output after content, naturally, no extra line breaks or spaces:
1 // output unknown number 2 int wantedAnsCount = 0 ; . 3 for ( int I = 0 ; I <ans.size (); ++ I) { . 4 IF (ANS [I]> 100 ) { . 5 IF (wantedAnsCount! = 0 ) COUT << endl; . 6 . 7 COUT << ANS [I]; . 8 } . 9 }