zoukankan      html  css  js  c++  java
  • 北京理工大学复试上机--2017

    1、输入身份证号,通过计算比较校验位来判断身份证号是否正确。最后一位 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>
    using namespace std;
    int main() {
        int wi[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        char z[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        string s;
        while (cin >> s) {
            int i, l, sum = 0;
            l = s.length() - 1;
            for (i = 0; i < 17; i++) sum += wi[i] * (s[i] - '0');
            if (z[sum % 11] == s[l]) cout << "正确" << endl;
            else cout << "应为:" << s.substr(0, 17) + z[sum % 11] << endl;
        }
        return 0;
    }
    

    PS:这题代码不知道为啥这么少,不知道自己有没有误解题目意思,视情况而定啊,考虑周全。要!


    2、显示出如下数组中的所有元素,并使用二分查找法在数组中查找元素。
    int a[]={-90,-32,12,16,24,36,45,59,98,120};
    输入:
    -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个元素为120,比较次数为4
    请输入所要查找的元素: 6
    查找失败,比较次数为3

    #include <iostream>
    #include <vector>
    using namespace std;
    int main() {
        vector<int> a;
        int n;
        while (cin >> n) {
            a.push_back(n);
            if(getchar() == '
    ') break;
        }
        for(int i = 0; i < a.size(); i++) {
            cout << a[i] << " ";
        }
        cout << endl << "请输入所要查找的元素: ";
        int cx;
        while(cin >> cx) {
            int low = 0;
            int high = a.size() - 1;
            int mid, cnt = 0;
            while(low <= high) {
                cnt++;
                mid = (low + high) / 2;
                if(a[mid] == cx) {
                    cout << "第" << mid + 1 << "个元素为" << cx << ",比较次数为" << cnt << endl;
                    break;
                }
                else if(a[mid] > cx) high = mid - 1;
                else low = mid + 1;
            }
            if(low > high) cout << "查找失败,比较次数为" << cnt << endl;
            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
    输出:
    *name:wangwu score:52 70 100
    *name:zhanger score:68 40 90
    [1] name:zhaoyi 70 80 91
    [2] name:lisi 70 80 90
    [3] name:wangwu 52 70 100
    [4] name:zhangsan 60 70 80
    [5] name:zhanger 68 40 90

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    struct student
    {
        string name;
        int g1, g2, g3;
        double avg;
    };
    
    bool cmp(student s1, student s2) {
        return s1.avg > s2.avg;
    }
    
    int main() {
        int n;
        cin >> n;
        student stu[n];
        vector<student> v;
        for(int i = 0; i < n; i++) {
            cin >> stu[i].name >> stu[i].g1 >> stu[i].g2 >> stu[i].g3;
            stu[i].avg = (stu[i].g1 + stu[i].g2 + stu[i].g3) / (double)3;
            if(stu[i].g1 < 60 || stu[i].g2 < 60 || stu[i].g3 < 60) v.push_back(stu[i]);
        }
        sort(stu, stu + n, cmp);
        sort(v.begin(), v.end(), cmp);
        for(int i = 0; i < v.size(); i++) {
            cout << "*name:" << v[i].name << " score:" << v[i].g1 << " " << v[i].g2 << " " << v[i].g3 << endl;
        }
        for(int i = 0; i < n; i++) {
            cout << "[" << i + 1 << "] name:" << stu[i].name << " " << stu[i].g1 << " " << stu[i].g2 << " " << stu[i].g3 << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    [LeetCode] Power of Three 判断3的次方数
    [LeetCode] 322. Coin Change 硬币找零
    [LeetCode] 321. Create Maximum Number 创建最大数
    ITK 3.20.1 VS2010 Configuration 配置
    VTK 5.10.1 VS2010 Configuration 配置
    FLTK 1.3.3 MinGW 4.9.1 Configuration 配置
    FLTK 1.1.10 VS2010 Configuration 配置
    Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别
    [LeetCode] Bulb Switcher 灯泡开关
    [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
  • 原文地址:https://www.cnblogs.com/ache/p/12599473.html
Copyright © 2011-2022 走看看