天天看點

PAT (Basic Level) Practice (中文)1061 判斷題 (15分)

1061 判斷題 (15分)

判斷題的評判很簡單,本題就要求你寫個簡單的程式幫助老師判題并統計學生們判斷題的得分。

輸入格式:

輸入在第一行給出兩個不超過 100 的正整數 N 和 M,分别是學生人數和判斷題數量。第二行給出 M 個不超過 5 的正整數,是每道題的滿分值。第三行給出每道題對應的正确答案,0 代表“非”,1 代表“是”。随後 N 行,每行給出一個學生的解答。數字間均以空格分隔。

3 6
2 1 3 3 4 5
0 0 1 0 1 1
0 1 1 0 0 1
1 0 1 0 1 0
1 1 0 0 1 1
           
13
11
12
           

總結

  1. 把malloc()和calloc()區分開:malloc申請一塊沒有初始化的空間,calloc申請到一塊空間并全部初始化為0。在這道題中,分數需要初始化為0,需要使用calloc
#include <stdio.h>
#include <stdlib.h>

int
main( int argc, char **argv )
{
	int i, j;
	int ans;
	int n, m;
	int *score, *standard, *mark;
	scanf("%d%d", &n, &m);
	
	score = ( int* )calloc( n, sizeof( int ) );
	standard = ( int* )malloc( sizeof( int ) * m );
	mark = ( int* )malloc( sizeof( int ) * m );

	for( i = 0; i < m; i++ ){
		scanf("%d", &mark[i] );
	}
	
	for( i = 0; i < m; i++ ){
		scanf("%d", &standard[i]);
	}
	
	for( i = 0; i < n; i++ ){
		for( j = 0; j < m; j++ ){
			scanf("%d", &ans);
			if( ans == standard[j] ){
				score[i] += mark[j];
			}
		}
	}
	
	for( i = 0; i < n; i++ ){
		printf("%d\n", score[i]);
	}

	free( score );
	free( standard );
	free( mark );

	return 0;
}