信息学奥赛一本通C++语言——1176:谁考了第k名

【题目描述】
在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第k名学生的学号和成绩。

【输入】
第一行有两个整数,分别是学生的人数n(1≤n≤100),和求第k名学生的k(1≤k≤n)。

其后有n行数据,每行包括一个学号(整数)和一个成绩(浮点数),中间用一个空格分隔。

【输出】
输出第k名学生的学号和成绩,中间用空格分隔。(注:请用%g输出成绩)

【输入样例】
5 3
90788001 67.8
90788002 90.3
90788003 61
90788004 68.4
90788005 73.9
【输出样例】
90788004 68.4
【源代码】

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct student { //定义结构体
	int id;
	double score;
} stu[110];
void mysort(int n);//排序函数
int main() {
	int n,k;
	cin>>n>>k;
	for(int i=1; i<=n; i++) {
		cin>>stu[i].id>>stu[i].score;//录入数据
	}
	mysort(n);//调用排序
	printf("%d %g",stu[k].id,stu[k].score);//输出
	return 0;
}
void mysort(int n) {//排序函数
	for(int i=1; i<=n; i++) {
		for(int j=i+1; j<=n; j++) {
			if(stu[i].score<stu[j].score) {
				swap(stu[i],stu[j]);//交换位置
			}
		}
	}
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于矩阵乘法,我们可以使用嵌套的循环来实现。假设有两个矩阵 A 和 B,A 是 m 行 n 列的矩阵,B 是 n 行 p 列的矩阵,它们的乘积矩阵 C 是 m 行 p 列的矩阵。 下面是一个简单的 C++ 代码示例,用于计算两个矩阵的乘积: ```cpp #include <iostream> using namespace std; const int MAX_SIZE = 100; void matrixMultiplication(int A[][MAX_SIZE], int B[][MAX_SIZE], int C[][MAX_SIZE], int m, int n, int p) { for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { C[i][j] = 0; for (int k = 0; k < n; k++) { C[i][j] += A[i][k] * B[k][j]; } } } } int main() { int m, n, p; cout << "Enter the number of rows of matrix A: "; cin >> m; cout << "Enter the number of columns of matrix A / rows of matrix B: "; cin >> n; cout << "Enter the number of columns of matrix B: "; cin >> p; int A[MAX_SIZE][MAX_SIZE], B[MAX_SIZE][MAX_SIZE], C[MAX_SIZE][MAX_SIZE]; cout << "Enter elements of matrix A:" << endl; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> A[i][j]; } } cout << "Enter elements of matrix B:" << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < p; j++) { cin >> B[i][j]; } } matrixMultiplication(A, B, C, m, n, p); cout << "Matrix A * Matrix B = " << endl; for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { cout << C[i][j] << " "; } cout << endl; } return 0; } ``` 在该示例中,我们首先输入两个矩阵 A 和 B 的维度,然后逐个输入它们的元素。最后,调用 `matrixMultiplication` 函数来计算矩阵乘法,并输出结果矩阵 C。 希望这个例子能帮到你!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值