DHU OJ | 基本練習-80 平均分 | 結構體

80 平均分

作者: 江寶釧時間限制: 1S章節: 結構體

問題描述 :

從鍵盤依次輸入每個學生的學號、姓名、出生年月、3門課的成績,計算並打印出每個學生的平均成績。

要求使用結構體數組。

 

輸入說明 :

第一行,整數n,表示一共有n個學生。

從第二行開始共n行,每行包含學號,姓名,出生年,出生月,數學,英語,C語言的成績,用空格分隔,姓名不含空格。

輸出說明 :

共n行,每行包含學號,姓名,出生年/月,數學,英語,C語言,平均成績。

輸出浮點數使用“%.0f”,出生年月用“/”分開,數據之間以一個空格分隔。

輸入範例 :

2
901 hulei 1990 8 67 78 89
902 fangang 1991 7 85 69 76

輸出範例 :

901 hulei 1990/8 67 78 89 78
902 fangang 1991/7 85 69 76 77
 

筆記

特別注意,在創建結構體變量時,要在結構體名稱前加上“struct”

struct student{
	int no;
	char name[10];
	int birth_y;
	int birth_m;
	int math_score;
	int e_score;
	int c_score;
};
struct student stu;

否則會出現編譯錯誤。

代碼

#include<stdio.h>
struct student{
	int no;
	char name[10];
	int birth_y;
	int birth_m;
	int math_score;
	int e_score;
	int c_score;
};

int main(){
	int n;
	scanf("%d",&n);
	
	int i;
	struct student stu;
	int avg_score = 0;
	for(i=0;i<n;i++){
		scanf("%d %s %d %d %d %d %d",
     &stu.no,stu.name,&stu.birth_y,&stu.birth_m,&stu.math_score,&stu.e_score,&stu.c_score);
		printf("%d %s %d/%d %d %d %d %.0f\n",
		stu.no,stu.name,stu.birth_y,stu.birth_m,stu.math_score,stu.e_score,stu.c_score,
				1.0*(stu.math_score+stu.e_score+stu.c_score)/3);
	}
	
	return 0;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章