如何統計學生成績?

本題要求編寫程序讀入N個學生的百分制成績,統計五分製成績的分佈。百分制成績到五分製成績的轉換規則:

**大於等於90分爲A;
小於90且大於等於80爲B;
小於80且大於等於70爲C;
小於70且大於等於60爲D;
小於60爲E。

輸入格式:

輸入在第一行中給出一個正整數N(≤1000),即學生人數;第二行中給出N個學生的百分制成績,其間以空格分隔。

輸出格式:

在一行中輸出A、B、C、D、E對應的五分製成績的人數分佈,數字間以空格分隔,行末不得有多餘空格。
輸入樣例:**

7
77 54 92 73 60 65 69

輸出樣例:

1 0 2 3 1

方法一

#include<stdio.h>
int main()
{
	int n, score;
	int A, B, C, D, E;
	A=B=C=D=E=0;
	scanf("%d", &n);
	for(int i=1; i<=n; i++){
		scanf("%d", &score);
		if(score>=90)
				A++;
		else if(score>=80)
				B++;
		else if(score>=70)
				C++;
		else if(score>=60)
				D++;
		else
			E++;
	}
	printf("%d %d %d %d %d", A, B, C, D, E);
	
	return 0;	
}

方法二,利用數組來存放等級

#include<stdio.h>
#include<stdlib.h>
int main()
{ 
    int a[5] = { 0 };//a[i]的值對應等級的人數個數
	int cnt = 0;//統計人數的個數
	int score = 0;//分數
	int n = 0;//總的人數
	int m = 0;//等級對應數組下標
	scanf("%d", &n);
	while (cnt < n)
	{
		scanf("%d", &score);
		if (score >= 90)
		{
			m = 0;
		}
		else if (score >= 80)
		{
			m = 1;
		}
		else if (score >= 70)
		{
			m = 2;
		}
		else if (score>=60)
		{
			m = 3;
		}
		else
		{
			m = 4;
		}
		a[m]++;
		cnt++;
	}
	//打印
	int i;
	for (i = 0; i < 4; i++)
	{
		printf("%d ", a[i]);
	}
	printf("%d", a[i]);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章