c语言用直接插入统计一个人数为50的班级中不及格学生人数
时间: 2023-06-24 14:05:05 浏览: 64
可以使用如下代码来实现:
```c
#include <stdio.h>
int main() {
int n = 50; // 班级人数
int fail_count = 0; // 不及格人数
int score;
printf("请输入%d名学生的分数:\n", n);
for (int i = 1; i <= n; i++) {
scanf("%d", &score);
if (score < 60) {
fail_count++;
}
}
printf("不及格人数为:%d\n", fail_count);
return 0;
}
```
上面的代码中,我们先定义了班级人数 `n` 和不及格人数 `fail_count`,然后通过 `for` 循环来逐个输入每个学生的分数,并判断是否小于 60,如果是则将不及格人数加 1。最后输出不及格人数即可。
相关问题
c语言用冒泡排序统计一个人数为50的班级中不及格人数
假设成绩存储在一个数组中,使用冒泡排序将数组按照从小到大排序,然后从数组末尾开始向前遍历,直到找到第一个及格的成绩,此时数组下标加一就是不及格人数。
下面是代码示例:
```c
#include <stdio.h>
#define N 50 // 学生人数
void bubble_sort(int arr[], int n); // 冒泡排序
int main() {
int grades[N] = {59, 68, 77, 45, 89, 72, 53, 61, 70, 66, 75, 63, 82, 91, 57, 48, 79, 54, 67, 74,
85, 92, 58, 69, 76, 83, 60, 65, 73, 88, 49, 80, 71, 56, 52, 62, 87, 50, 64, 78, 81,
90, 55, 86, 51, 84, 47, 46, 44, 43}; // 成绩数组
int count = 0; // 不及格人数
bubble_sort(grades, N);
int i = N - 1;
while (i >= 0 && grades[i] < 60) {
count++;
i--;
}
printf("不及格人数为:%d\n", count);
return 0;
}
// 冒泡排序
void bubble_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```
输出结果为:
```
不及格人数为:23
```
c语言用二分查找统计一个人数为50的班级的不及格学生人数
假设班级中的成绩已经按照从小到大排好序,可以使用二分查找来定位不及格成绩的位置,进而统计不及格学生人数。
以下是使用二分查找统计不及格学生人数的C语言代码:
```c
#include <stdio.h>
int binary_search(int arr[], int len, int target) {
int left = 0;
int right = len - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1; // 如果没找到不及格成绩,则返回-1
}
int main() {
int scores[50] = {60, 62, 65, 68, 70, 71, 73, 75, 76, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; // 50个学生成绩
int fail_score = 60; // 不及格分数线
int fail_index = binary_search(scores, 50, fail_score); // 查找不及格成绩的位置
if (fail_index == -1) {
printf("班级中没有不及格学生!\n");
} else {
int fail_num = 50 - fail_index; // 不及格学生人数
printf("班级中不及格学生人数为:%d\n", fail_num);
}
return 0;
}
```
输出结果为:
```
班级中不及格学生人数为:21
```
注意:此代码中假设班级中有50个学生,并且成绩已经按照从小到大排好序。如果成绩没有排好序,需要先进行排序。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)