#include <stdio.h> #define MAXN 10 struct student{ int num; char name[20]; int score; char grade; }; int set_grade( struct student *p, int n ); int main() { struct student stu[MAXN], *ptr; int n, i, count; ptr = stu; scanf("%d\n", &n); for(i = 0; i < n; i++){ scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score); } count = set_grade(ptr, n); printf("The count for failed (<60): %d\n", count); printf("The grades:\n"); for(i = 0; i < n; i++) printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade); return 0; }
时间: 2024-04-28 19:26:09 浏览: 7
这是一段 C 语言代码,定义了一个名为 `student` 的结构体,用于存储学生的学号、姓名、成绩和等级。其中 `set_grade` 函数用于计算每个学生的等级,并返回不及格(分数小于 60)的学生数量。在 `main` 函数中,首先读入学生数量 `n`,然后读入每个学生的学号、姓名和成绩,并调用 `set_grade` 函数计算等级和不及格学生数量,并输出结果。最后输出每个学生的学号、姓名和等级。
相关问题
#include<stdio.h> #define MAXN 100 struct student{ char name[20]; int score; }; void cnt_score( struct student *p, int n ); int main() { int i, n; struct student stu[MAXN]; scanf("%d", &n); for(i = 0; i < n; i++){ scanf("%s%d", stu[i].name, &stu[i].score); } cnt_score(stu, n); return 0; } void cnt_score(struct student *p, int n) { int cnt_a = 0, cnt_p = 0, cnt_f = 0; while ( p <= q ){ if ( ) cnt_a++; else if ( ) cnt_p++; else cnt_f++; p++; } printf("%d %d %d\n", cnt_a, cnt_p, cnt_f); }
这段代码中存在一些错误,需要进行修改:
1. 函数 `cnt_score` 中的参数 `q` 没有定义,应该改为 `n`。
2. 在 `while` 循环中的 `if` 判断条件中缺少具体的判断表达式,需要根据题意进行填写。
修改后的代码如下:
```
#include <stdio.h>
#define MAXN 100
struct student{
char name[20];
int score;
};
void cnt_score(struct student *p, int n);
int main(){
int i, n;
struct student stu[MAXN];
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%s%d", stu[i].name, &stu[i].score);
}
cnt_score(stu, n);
return 0;
}
void cnt_score(struct student *p, int n){
int cnt_a = 0, cnt_p = 0, cnt_f = 0;
while (n--){
if (p->score >= 90) cnt_a++;
else if (p->score >= 60) cnt_p++;
else cnt_f++;
p++;
}
printf("%d %d %d\n", cnt_a, cnt_p, cnt_f);
}
```
这个函数的功能是统计学生成绩的分布情况,分为优秀、及格和不及格三类。其中,成绩大于等于 90 分为优秀,成绩大于等于 60 分小于 90 分为及格,成绩小于 60 分为不及格。
第1关:非递归实现皇后问题#include <stdio.h> /********** Begin **********/ void main() { } /********** End **********/
皇后问题是指在一个n×n的棋盘上放置n个皇后,使得皇后之间互相不能攻击到,即任意两个皇后都不能处于同一行、同一列或同一斜线上。下面是一个非递归实现皇后问题的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int n, ans, cur;
int col[MAXN], d1[MAXN * 2], d2[MAXN * 2];
struct Node {
int row, col;
} stack[MAXN];
void push(int row, int col) {
stack[cur].row = row;
stack[cur].col = col;
cur++;
col[row] = d1[row + col] = d2[row - col + n] = 1;
}
void pop() {
cur--;
int row = stack[cur].row;
int col = stack[cur].col;
col[row] = d1[row + col] = d2[row - col + n] = 0;
}
void dfs() {
while (cur > 0 || stack[0].row < n) {
if (stack[cur - 1].row == n - 1) {
ans++;
pop();
} else {
int row = stack[cur - 1].row + 1;
int col = 0;
while (col < n && (col[row] || d1[row + col] || d2[row - col + n])) {
col++;
}
if (col < n) {
push(row, col);
} else {
pop();
}
}
}
}
int main() {
scanf("%d", &n);
dfs();
printf("%d\n", ans);
return 0;
}
```