2017年北理复试上机
1、输入身份证号,通过计算比较校验位来判断身份证号是否正确。
如,aaaaaayyyymmddxxsp共18位,其中:
年份代码yyyy共4位。最后一位p为校验位。
校验规则是:
(1)对前17位数字的权求和 S=Sum(Ai*Wi),i=0,...,16
Ai:表示第i位置上的身份证号码数字值
Wi:表示第i位置上的加权因子
Wi:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2)计算模 Y=mod(S,11)
(3)通过模得到对应的校验码
Y:0 1 2 3 4 5 6 7 8 9 10
校验码:1 0 X 9 8 7 6 5 4 3 2
例如,如果得到Y为9则最后的校验位p应该为3
如果校验位不是3,则该身份证号码不正确。
输入示例:
110130197606175317
输出示例:
110130197606175317 正确.
输入示例:
110200197501175220
输出示例:
应为:11020019750117522X
#include<iostream> #include<string> using namespace std; int main() { string s; cin >> s; int sum = 0; int w[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 += (s[i] - '0')*w[i]; int y = sum % 11; char a[11] = { '1','0','X','9','8','7','6','5','4','3','2' }; if (s[17] == a[y])cout << s << " 正确" << endl; else { cout << "应为:"; for (int i = 0; i < 17; i++)cout << s[i]; cout << a[y] << endl; } return 0; }
2、显示出如下数组中的所有元素,并使用二分查找法在数组中查找元素。
int a[]={-90,-32,12,16,24,36,45,59,98,120};
输出示例:
-90 -32 12 16 24 36 45 59 98 120
请输入所要查找的元素:24
输出:第5个元素为24,比较次数为1
请输入所要查找的元素:120
输出:第10个元素,比较次数为4
请输入所要查找的元素:6
输出:查找失败 比较次数为3
#include<iostream> using namespace std; int a[10] = { -90,-32,12,16,24,36,45,59,98,120 }; void search(int x) { int b = 0, e = 9, m; int cnt = 0; while (b <= e) { cnt++; m = (b + e) / 2; if (a[m] == x) { cout << "第" << m + 1 << "个元素为" << x << ",比较次数为" << cnt << endl; return; } else if (a[m] > x)e = m - 1; else if (a[m] < x)b = m + 1; } cout << "查找失败,比较次数为" << cnt << endl; return; } int main() { int x; cout << a[0]; for (int i = 1; i < 10; i++)cout << " " << a[i]; cout << endl; cout << "请输入所要查找的元素:"; while (cin >> x) { search(x); cout << "请输入所要查找的元素:"; } return 0; }
3、输入学生个数以及每个学生的姓名和3门课程成绩:输出不及格学生的信息;按平均成绩排序,从高到低输出学生信息。
输入示例:
5
zhaoyi 70 80 91
zhanger 68 40 90
zhangsan 60 70 80
lisi 70 80 90
wangwu 52 70 100
输出示例:
*[zhanger] 68 40 99
*[wangwu] 52 70 100
zhaoyi 70 80 91
lisi 70 80 90
wangwu 52 70 100
zhangsan 60 70 80
zhanger 68 40 99
#include<iostream> #include<string> #include<algorithm> using namespace std; struct Student { int score[3]; string name; double avg; }t[50]; bool cmp(Student a, Student b) { return a.avg > b.avg; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { double total = 0; cin >> t[i].name >> t[i].score[0] >> t[i].score[1] >> t[i].score[2]; for (int j = 0; j < 3; j++) total += t[i].score[j]; t[i].avg = total / 3; } for (int i = 0; i < n; i++) { if (t[i].score[0] < 60 || t[i].score[1] < 60 || t[i].score[1] < 60)cout << "*[" << t[i].name << "] " << t[i].score[0] << " " << t[i].score[1] << " " << t[i].score[2] << endl; } sort(t, t + n, cmp); for (int i = 0; i < n; i++)cout << t[i].name << " " << t[i].score[0] << " " << t[i].score[1] << " " << t[i].score[2] << endl; return 0; }