PAT-乙级 1012 数字分类(C语言)

这个题目如果总是有个测试点过不去就是因为A2类的交错求和的结果本身就有可能是0,而把0作为判断这一类数字不存在的条件是错误的。(转自https://blog.csdn.net/LIUJIALING_lala/article/details/81170544)

#include 

int main() {
	int numbers,i,num,n=1,intMax=0;
	scanf("%d", &numbers);
	int value[5] = { 0 }, count[5] = { 0 };
	int *numberPointer;
	numberPointer = (int *)malloc(4 * numbers);
	for (i = 0; i < numbers; i++)
		scanf("%d", (numberPointer + i));
	for (i = 0; i < numbers; i++) {
		num = *(numberPointer + i) % 5;
		switch (num) {
		case 0:if (*(numberPointer + i) % 2 == 0) { value[num] += *(numberPointer + i) ; count[num]++; }break;
		case 1:value[num] += *(numberPointer + i)*n; n *= -1; count[num]++; break;
		case 2:count[num]++; break;
		case 3:value[num] += *(numberPointer + i); count[num]++; break;
		case 4:if (*(numberPointer + i) > intMax) { intMax = *(numberPointer + i); value[num] = *(numberPointer + i); count[num]++; }break;
		}
	}
	//输出
	for (i = 0; i < 5; i++) {
		if (count[i] == 0)
			printf("N");
		else if (i == 0 || i == 1 || i == 4)
			printf("%d", value[i]);
		else if (i == 2)
			printf("%d", count[i]);
		else if (i == 3)
			printf("%.1lf", (double)value[3] / count[3]);
		if (i != 4)
			printf(" ");
	}
	free(numbersPointer);
	//system("pause");
	return 0;
}

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