PAT-乙级 1038 统计同成绩学生(C语言)

首先给出自己做法,也是大多数的做法,但是最后一个测试点死活过不去,考虑到是两个for循环占用了绝大多数时间。后来再看题目时,发现分数是百分制,最后解决

#include
#include
int main() {
	int studentNumber;
	scanf("%d", &studentNumber);
	int* studentPointer = calloc(sizeof(int), studentNumber);
	int i,j;
	for (i = 0; i < studentNumber; i++) 
		scanf("%d", (studentPointer + i));
	int queryNumber;
	scanf("%d", &queryNumber);
	int* queryPointer = calloc(sizeof(int), queryNumber);
	int* countPointer = calloc(sizeof(int), queryNumber);
	for (i = 0; i < queryNumber; i++)
		scanf("%d", (queryPointer+i));
	for (i = 0; i < studentNumber; i++) {
		for (j = 0; j < queryNumber; j++) {
			if (*(studentPointer + i) == *(queryPointer + j))
				*(countPointer + j)+=1;
		}
	}
	for (i = 0; i < queryNumber; i++) {
		printf("%d", *(countPointer + i));
		if (i != queryNumber - 1)
			printf(" ");
	}
	//system("pause");
	return 0;
}

更正:2020年6月21日01:11:42

#include
#include
int main() {
	int count[101] = { 0 };
	int studentNumber, i, middle, queryNumber;
	scanf("%d", &studentNumber);
	for (i = 0; i < studentNumber; i++) {
		scanf("%d", &middle);
		count[middle]++;
	}
	scanf("%d", &queryNumber);
	int* queryPointer = calloc(sizeof(int), queryNumber);
	for (i = 0; i < queryNumber; i++) {
		scanf("%d", (queryPointer + i));
		printf("%d", count[*(queryPointer + i)]);
		if (i != queryNumber - 1)
			printf(" ");
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(PAT-乙级(C语言))