Skip to content

Commit

Permalink
🔨 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Mar 21, 2019
1 parent db87954 commit 6931e93
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions BasicLevel_C++/1031. 查验身份证(15).cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
#include <iostream>
#include <string>
using namespace std;
bool func(string a);
int a[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
int b[11] = {1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2};
string s;
bool isTrue() {
int sum = 0;
for (int i = 0; i < 17; i++) {
if (s[i] < '0' || s[i] > '9') return false;
sum += (s[i] - '0') * a[i];
}
int temp = (s[17] == 'X') ? 10 : (s[17] - '0');
return b[sum%11] == temp;
}
int main() {
int n, count = 0;
int n, flag = 0;
cin >> n;
for (int i = 0; i < n; i++) {
string a;
cin >> a;
if (func(a)) count++;
}
if (count == 0) cout << "All passed";
return 0;
}
bool func(string s) {
int sum = 0, a[18];
for (int i = 0; i < 17; i++) {
if (s[i] >= '0' && s[i] <= '9') {
a[i] = s[i] - '0';
} else {
cin >> s;
if (!isTrue()) {
cout << s << endl;
return true;
break;
flag = 1;
}
}
a[17] = (s[17] == 'X') ? 10 : (s[17] - '0');
int b[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
for (int i = 0; i < 17; i++)
sum = sum + a[i] * b[i];
sum = sum % 11;
int c[11] = {1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2};
if (c[sum] != a[17]) {
cout << s << endl;
return true;
}
return false;
if (flag == 0) cout << "All passed";
return 0;
}

0 comments on commit 6931e93

Please sign in to comment.