本题要求编写程序,计算学生们的平均成绩,并统计及格(成绩不低于60分)的人数。题目保证输入与输出均在整型范围内。
输入格式:
输入在第一行中给出非负整数N,即学生人数。第二行给出N个非负整数,即这N位学生的成绩,其间以空格分隔。
输出格式:
average = 成绩均值
count = 及格人数
其中平均值精确到小数点后一位。
输入样例1:
5
77 54 92 73 60
输出样例1:
average = 71.2
count = 4
答案代码块
#include <stdio.h>
int main(){
int n,i;
double average=0.0;
int count = 0 ,x;
scanf("%d",&n);
if(n != 0){
for(i=0;i<n;i++){
scanf("%d",&x);
if(x>=60){
count++;
}
average+=x;
}
average /= n*1.0;
}
printf("average = %.1f\n",average );
printf("count = %d",count );
//或printf("average = %.1f\ncount = %d",average,count);
return 0;
}
答案代码块:方法二 链表
#include <stdio.h>
struct ListNode {
int x;
struct ListNode *pNext;
};
struct ListNode *getList(int n);
struct ListNode *readList(struct ListNode *L,int n);
int main(){
int n,i;
struct ListNode *L ,*R;
scanf("%d",&n);
L = getList(n);
R = readList(L,n);
return 0;
}
struct ListNode *getList(int n){//生成链表赋值
struct ListNode *pEnd = NULL;
struct ListNode *pHead =NULL;
struct ListNode *pNew;
int x;
while(n != 0){
scanf("%d",&x);
pNew = (struct ListNode *)malloc(sizeof(struct ListNode));
if(pNew != NULL){
pNew->x = x;
pNew->pNext = NULL;
}
if( pHead != NULL){
pEnd->pNext = pNew;
}else{
pHead = pNew;
}
pEnd = pNew;
n--;
}
return pHead;
}
struct ListNode *readList(struct ListNode *L,int n){
double average = 0.0;
int count = 0;
if(n!=0){//如果总数是0则不执行
for( L ; L != NULL ; L=L->pNext){//遍历链表
if(L->x>=60){
count++;
}
average+=L->x;
}
average /= n*1.0;
}
printf("average = %.1f\n",average );
printf("count = %d",count );
}