题目描述 在一次考试中,每个考生的成绩都不相同,要求输入每个考生的学号和成绩,求考第n名同学的学号和成绩。

输入 输入学生人数为a(2 <= a <= 10),输入每名学生学号为1、2、3...以及对应学生的成绩,n为需要查找的考第n名同学的名次

输出 输出包含查找的序号以及成绩

样例 【输入样例1】 5 1 99 2 95 3 94 4 98 5 100 1

【输出样例1】 5 100

7 条评论

  • @ 2026-4-7 17:00:13
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int MAX_LEN = 1000;  // 最大支持1000位数字
    
    // ======================== 工具函数 ========================
    // 字符串转逆序整型数组("123" → [3,2,1])
    void strToBigInt(const char str[], int num[], int &len) {
        len = strlen(str);
        memset(num, 0, sizeof(int) * MAX_LEN);
        for (int i = 0; i < len; ++i) {
            num[i] = str[len - 1 - i] - '0';
        }
    }
    
    // 逆序整型数组转字符串([3,2,1] → "123")
    void bigIntToStr(int num[], int len, char str[]) {
        while (len > 1 && num[len - 1] == 0) len--;
        int idx = 0;
        for (int i = len - 1; i >= 0; --i) {
            str[idx++] = num[i] + '0';
        }
        str[idx] = '\0';
    }
    
    // 比较 a 是否 >= b
    bool bigIntGreaterEqual(int a[], int aLen, int b[], int bLen) {
        if (aLen != bLen) return aLen > bLen;
        for (int i = aLen - 1; i >= 0; --i) {
            if (a[i] != b[i]) return a[i] > b[i];
        }
        return true;
    }
    
    // ======================== 四则运算函数 ========================
    // 1. 加法:a + b = res
    void bigIntAdd(int a[], int aLen, int b[], int bLen, int res[], int &resLen) {
        memset(res, 0, sizeof(int) * MAX_LEN);
        resLen = max(aLen, bLen);
        int carry = 0;
        for (int i = 0; i < resLen; ++i) {
            int sum = a[i] + b[i] + carry;
            res[i] = sum % 10;
            carry = sum / 10;
        }
        if (carry) res[resLen++] = carry;
    }
    
    // 2. 减法:a - b = res(必须 a >= b)
    void bigIntSub(int a[], int aLen, int b[], int bLen, int res[], int &resLen) {
        memset(res, 0, sizeof(int) * MAX_LEN);
        resLen = max(aLen, bLen);
        int borrow = 0;
        for (int i = 0; i < resLen; ++i) {
            int sub = a[i] - b[i] - borrow;
            borrow = 0;
            if (sub < 0) {
                sub += 10;
                borrow = 1;
            }
            res[i] = sub;
        }
    }
    
    // 3. 乘法:a * b = res
    void bigIntMul(int a[], int aLen, int b[], int bLen, int res[], int &resLen) {
        memset(res, 0, sizeof(int) * MAX_LEN);
        for (int i = 0; i < aLen; ++i) {
            int carry = 0;
            for (int j = 0; j < bLen || carry; ++j) {
                int val = res[i + j] + a[i] * (j < bLen ? b[j] : 0) + carry;
                res[i + j] = val % 10;
                carry = val / 10;
            }
        }
        resLen = aLen + bLen;
        while (resLen > 1 && res[resLen - 1] == 0) resLen--;
    }
    
    // 4. 除法:a / b = res(整除,无小数)
    void bigIntDiv(int a[], int aLen, int b[], int bLen, int res[], int &resLen) {
        memset(res, 0, sizeof(int) * MAX_LEN);
        int tmp[MAX_LEN] = {0}, tmpLen = 0;
        for (int i = aLen - 1; i >= 0; --i) {
            for (int j = tmpLen; j > 0; --j) tmp[j] = tmp[j - 1];
            tmp[0] = a[i];
            tmpLen = (tmpLen == 0 && tmp[0] != 0) ? 1 : tmpLen + 1;
            
            int cnt = 0;
            while (bigIntGreaterEqual(tmp, tmpLen, b, bLen)) {
                int t[MAX_LEN], tl;
                bigIntSub(tmp, tmpLen, b, bLen, t, tl);
                memcpy(tmp, t, sizeof(int) * MAX_LEN);
                tmpLen = tl;
                cnt++;
            }
            res[i] = cnt;
        }
        resLen = aLen;
        while (resLen > 1 && res[resLen - 1] == 0) resLen--;
    }
    int main() {
    
        char num1[MAX_LEN] = "12345678901234567890";
        char num2[MAX_LEN] = "98765432109876543210";
        char result[MAX_LEN];
        int a[MAX_LEN], b[MAX_LEN], res[MAX_LEN];
        int aLen, bLen, resLen;
        strToBigInt(num1, a, aLen);
        strToBigInt(num2, b, bLen);
        bigIntAdd(a, aLen, b, bLen, res, resLen);    // 加法
        bigIntSub(a, aLen, b, bLen, res, resLen);    // 减法
        bigIntMul(a, aLen, b, bLen, res, resLen);    // 乘法
        bigIntDiv(a, aLen, b, bLen, res, resLen);    // 除法
    
        bigIntToStr(res, resLen, result);
        cout << result << endl;
        return 0;
    }
    
    • @ 2026-1-6 13:54:07

      (又又又又又又又又一个代码)

      • @ 2026-1-6 13:53:19

        #include #include #include

        // 定义学生结构体,存储学号和成绩 struct Student { int id; // 学号 int score; // 成绩 };

        // 自定义排序规则:按成绩降序排列 bool compare(const Student& s1, const Student& s2) { return s1.score > s2.score; }

        int main() { int a; // 学生人数 std::cin >> a;

        // 存储所有学生信息
        std::vector<Student> students;
        for (int i = 0; i < a; ++i) {
            int id, score;
            std::cin >> id >> score;
            students.push_back({id, score});
        }
        
        int n;  // 要查找的名次
        std::cin >> n;
        
        // 按成绩降序排序
        std::sort(students.begin(), students.end(), compare);
        
        // 输出第n名的学号和成绩(数组下标为n-1)
        std::cout << students[n-1].id << " " << students[n-1].score << std::endl;
        
        return 0;
        

        }

        • @ 2025-12-31 15:08:10

          核心逻辑是结构体存储 + 自定义规则排序 + 下标查找,利用 std::sort 实现高效排序,适合题目中 2~10 人的小规模数据。 注意数组下标和名次的对应关系:第 n 名对应数组下标 n-1,这是新手容易出错的点。 代码使用 vector 存储学生信息,相比普通数组更灵活,无需提前固定长度,适配题目中 2~10 的人数范围。

          • @ 2025-12-31 15:07:49

            结构体定义:Student 结构体把学号(id)和成绩(score)绑定,方便后续排序和查找。 排序规则:compare 函数是排序的核心,返回 s1.score > s2.score 表示成绩高的学生排在前面。 输入处理:先读入学生人数 a,再循环读入 a 个学生的学号和成绩,最后读入要查找的名次 n。 查找输出:排序后数组的第 n-1 个元素就是第 n 名的学生(因为数组下标从 0 开始),直接输出其学号和成绩即可。

            • @ 2025-12-31 15:05:53

              #include #include #include

              // 定义学生结构体,存储学号和成绩 struct Student { int id; // 学号 int score; // 成绩 };

              // 自定义排序规则:按成绩降序排列 bool compare(const Student& s1, const Student& s2) { return s1.score > s2.score; }

              int main() { int a; // 学生人数 std::cin >> a;

              // 存储所有学生信息
              std::vector<Student> students;
              for (int i = 0; i < a; ++i) {
                  int id, score;
                  std::cin >> id >> score;
                  students.push_back({id, score});
              }
              
              int n;  // 要查找的名次
              std::cin >> n;
              
              // 按成绩降序排序
              std::sort(students.begin(), students.end(), compare);
              
              // 输出第n名的学号和成绩(数组下标为n-1)
              std::cout << students[n-1].id << " " << students[n-1].score << std::endl;
              
              return 0;
              

              }

              • @ 2025-12-25 16:26:44

                #include<bits/stdc++.h> using namespace std; struct node{ int hao; int fen; }; bool cmp(node a,node b){ return a.fen>b.fen; } int main(){ int n; cin>>n; node a[1005]; for(int i=1;i<=n;i++){ cin>>a[i].hao; cin>>a[i].fen; } sort(a+1,a+1+n,cmp); int v; cin>>v; cout<<a[v].hao<<" "<<a[v].fen; }

                • 1