编写程序实现下述功能:从键盘输入一个学生成绩,然后将其插入到一个降序排列的成绩表中,插入后的成绩表仍然保持降序,最后输出结果。
时间: 2023-03-29 12:02:02 浏览: 104
您好,我可以回答这个问题。以下是实现该功能的程序:
#include <stdio.h>
int main() {
int score, i, j;
int grades[10] = {}; // 假设最多只有10个成绩
// 从键盘输入成绩
printf("请输入一个学生成绩:");
scanf("%d", &score);
// 将成绩插入到降序排列的成绩表中
for (i = ; i < 10; i++) {
if (score > grades[i]) {
// 将成绩插入到第i个位置
for (j = 9; j > i; j--) {
grades[j] = grades[j-1];
}
grades[i] = score;
break;
}
}
// 输出结果
printf("降序排列的成绩表为:");
for (i = ; i < 10; i++) {
printf("%d ", grades[i]);
}
printf("\n");
return ;
}
相关问题
编写程序实现下述功能:输入并记录10个学生的c语言程序编写程序找出其中的最高分,并将最高分与第一个成绩交换位置。然后将10名同学的成绩输出
好的,下面是实现上述功能的 Python 代码:
```python
# 输入10个学生的成绩
scores = []
for i in range(10):
score = int(input("请输入第%d个学生的成绩:" % (i+1)))
scores.append(score)
# 找出最高分并与第一个成绩交换位置
max_score = max(scores)
max_index = scores.index(max_score)
scores[0], scores[max_index] = scores[max_index], scores[0]
# 输出10名同学的成绩
print("最高分为:%d" % max_score)
print("10名同学的成绩为:")
for i in range(10):
print("第%d个学生的成绩为:%d" % (i+1, scores[i]))
```
运行程序后,可以依次输入10个学生的成绩,程序会自动找出最高分并将其与第一个成绩交换位置,然后输出10名同学的成绩,其中最高分会单独输出。
c语言编写程序实现下述功能:输入并记录10个学生的c语言程序编写程序找出其中的最高分,并将最高分与第一个成绩交换位置。然后将10名同学的成绩输出
好的,下面是实现上述功能的 C 语言代码:
```c
#include <stdio.h>
int main() {
int scores[10];
int i, max_score, max_index, temp;
// 输入10个学生的成绩
for (i = 0; i < 10; i++) {
printf("请输入第%d个学生的成绩:", i+1);
scanf("%d", &scores[i]);
}
// 找出最高分并与第一个成绩交换位置
max_score = scores[0];
max_index = 0;
for (i = 1; i < 10; i++) {
if (scores[i] > max_score) {
max_score = scores[i];
max_index = i;
}
}
temp = scores[0];
scores[0] = scores[max_index];
scores[max_index] = temp;
// 输出10名同学的成绩
printf("最高分为:%d\n", max_score);
printf("10名同学的成绩为:\n");
for (i = 0; i < 10; i++) {
printf("第%d个学生的成绩为:%d\n", i+1, scores[i]);
}
return 0;
}
```
运行程序后,可以依次输入10个学生的成绩,程序会自动找出最高分并将其与第一个成绩交换位置,然后输出10名同学的成绩,其中最高分会单独输出。