- 问答
hm4201求解
- @ 2025-12-24 20:42:53
题目描述 在一次考试中,每个考生的成绩都不相同,要求输入每个考生的学号和成绩,求考第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 条评论
-
-
#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; } -
#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;}
-
#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;}
-
#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