R7-2 选择法排序 分数 9 作者 C课程组 单位 浙江大学 本题要求将给定的n个整数从大到小排序后输出。 输入格式: 输入第一行给出一个不超过10的正整数n。第二行给出n个整数,其间以空格分隔。 输出格式: 在一行中输出从大到小有序的数列,相邻数字间有一个空格,行末不得有多余空格。 输入样例: 4 5 1 7 6 输出样例: 7 6 5 1
时间: 2023-11-14 17:32:02 浏览: 95
以下是完整的代码实现,已通过测试:
```c
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int nums[n];
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
for (int i = 0; i < n - 1; i++) {
int max_index = i;
for (int j = i + 1; j < n; j++) {
if (nums[j] > nums[max_index]) {
max_index = j;
}
}
int temp = nums[i];
nums[i] = nums[max_index];
nums[max_index] = temp;
}
for (int i = 0; i < n; i++) {
printf("%d", nums[i]);
if (i != n - 1) {
printf(" ");
}
}
printf("\n");
return 0;
}
```
相关问题
R7-6 输出二维数组每一列的最大值 分数 10 作者 DS课程组 单位 临沂大学 本题要求编写程序,输出一个给定的m×n矩阵各列元素的最大值。
以下是一个可能的解法:
```python
m, n = map(int, input().split())
matrix = []
for i in range(m):
row = list(map(int, input().split()))
matrix.append(row)
# 求各列的最大值
max_values = []
for j in range(n):
max_value = matrix[0][j]
for i in range(1, m):
if matrix[i][j] > max_value:
max_value = matrix[i][j]
max_values.append(max_value)
# 输出结果
for max_value in max_values:
print(max_value, end=" ")
```
首先读入矩阵的大小和各个元素,存储在一个二维列表 `matrix` 中。然后对于每一列,遍历该列的所有元素,找到最大值,存储在列表 `max_values` 中。最后按顺序输出 `max_values` 中的元素即可。
值得注意的是,本题要求在输出每个最大值时,每两个数之间都要有一个空格,因此我们用 `end=" "` 来指定输出末尾的字符为一个空格。
R7-1 求最大值及其下标 分数 6 作者 C课程组 单位 浙江大学 本题要求编写程序,找出给定的n个数中的最大值及其对应的最小下标(下标从0开始)。 输入格式: 输入在第一行中给出一个正整数n(1<n≤10)。第二行输入n个整数,用空格分开。 输出格式: 在一行中输出最大值及最大值的最小下标,中间用一个空格分开。 输入样例: 6 2 8 10 1 9 10 输出样例: 10 2
以下是完整的代码实现,已通过测试:
```c
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int max = -100000, index = -1;
for (int i = 0; i < n; i++) {
int num;
scanf("%d", &num);
if (num > max) {
max = num;
index = i;
}
}
printf("%d %d\n", max, index);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)