...平均值,并将他们的成绩按从大到小顺序输出?
发布网友
发布时间:2024-10-22 12:28
我来回答
共3个回答
热心网友
时间:2024-11-17 11:01
#include<stdio.h>
main()
{
int n,i;
int A=0,B=0,C=0,D=0,E=0;//用来统计各阶段学生成绩的人数,初始值都为0;
float score;//定义成绩变量;
float ave=0;//定义平均成绩
float num=0;//定义全部成绩总和
scanf("%d",&n);//从键盘输入学生成绩的个数
for(i=1;i<=n;i++)
{
scanf("%f",&score);//输入每个学生的成绩,共有n个学生
num+=score;//计算总成绩
switch(score)//switch 语句来计算各个分数段的成绩个数
{
case score>=90&&score<=100:A++;
case score>=80&&score<=89:B++;
case score>=70&&score<=79:C++;
case score>=60&&score<=69:D++;
case score>=0&&score<=59:E++;
default:break;
}
}
ave=num/n;//计算平均成绩
printf("%f",ave);//输出平均成绩
printf("%d",A);//输出成绩在90-100的个数
printf("%d",B);//输出成绩在80-89的个数
printf("%d",C);//输出成绩在70-79的个数
printf("%d",D);//输出成绩在60-69的个数
printf("%d",E);//输出成绩在0-59的个数
}
这样的函数是可以的。
热心网友
时间:2024-11-17 10:57
编程的内容非常的高深,建议你多向师傅请教一下。
热心网友
时间:2024-11-17 11:03
// https://zhidao.baidu.com/question/1181031506805258499.html?entry=uhome_homecenter_myTag
#include <stdio.h>
#include <stdlib.h>
#ifndef null
#define null ((void *)0)
#endif
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
typedef int bool;
typedef struct node Node;
typedef Node *LinkedList;
struct node {
int v;
Node *next;
};
bool is_null(LinkedList list) {
return list == null;
}
size_t llist_len(LinkedList list);
int *llist_toArray(LinkedList list) {
size_t n = llist_len(list);
int *base = (int *)malloc(sizeof(int) * n);
Node *p = list;
int i = 0;
while (p) {
base[i++] = p->v;
p = p->next;
}
return base;
}
LinkedList llist_init() {
LinkedList list = (LinkedList)null;
return list;
}
void llist_destroy(LinkedList list) {
Node *p = list;
Node *q = p->next;
while (q) {
free(p);
p = q;
q = q->next;
}
}
LinkedList llist_push(LinkedList list, int v) {
Node *node = (Node *)malloc(sizeof(Node));
node->v = v;
if (is_null(list)) {
list = node;
return list;
}
Node *p = list;
while (p->next) {
p = p->next;
}
p->next = node;
return list;
}
void llist_print(LinkedList list) {
Node *p = list;
printf("%d", p->v);
while (p->next) {
p = p->next;
printf(",%d", p->v);
}
printf("\n");
}
void array_print(int *a, int n) {
printf("%d", a[0]);
int i = 1;
for (; i < n; i++) {
printf(",%d", a[i]);
}
printf("\n");
}
size_t llist_len(LinkedList list) {
Node *p = list;
size_t len = 0;
while (p) {
len += 1;
p = p->next;
}
return len;
}
int llist_reduce(LinkedList list, int (*fn)(int acc, int c), int init) {
Node *p = list;
int acc = init;
while (p) {
acc = fn(acc, p->v);
p = p->next;
}
return acc;
}
int sumFn(int acc, int c) {
return acc + c;
}
// void qsort(void *base, size_t nmemb, size_t size,
// int (*compar)(const void *, const void *));
int compare(const void *a, const void *b) {
// return *(int *)a - *(int *)b;
return *(int *)b - *(int *)a;
}
int getInt() {
int n = 0;
scanf("%d", &n);
return n;
}
int main() {
// int n = getInt();
// printf("%d\n", n);
int i, n = 0;
n = getInt();
LinkedList list = llist_init();
for (; i < n; i++) {
list = llist_push(list, getInt());
}
// list = llist_push(list, 5);
// list = llist_push(list, 8);
// list = llist_push(list, 2);
// llist_print(list);
// printf("len=%lu\n", llist_len(list));
// size_t n = llist_len(list);
int sum = llist_reduce(list, sumFn, 0);
printf("avg=%f\n", (float)sum / n);
int *a = llist_toArray(list);
// 从大到小排
qsort(a, n, sizeof(int), compare);
llist_destroy(list);
array_print(a,n);
return 0;
}