代码re问题咨询
查看原帖
代码re问题咨询
881577
zx142407789楼主2023/6/22 17:23
#include<stdio.h>
#include<stdlib.h>
#define SIZE 7
typedef struct {
	int num[SIZE];
}lottery;
int main() {
	int out[SIZE] = { 0,0,0,0,0,0,0 };
	int n = 0;
	scanf("%d", &n);
	lottery* input = (lottery*)malloc(sizeof(lottery) * (n+1));//第12行
	for (int i = 0; i < n + 1; i++) {
		for (int j = 0; j < SIZE; j++) {
			scanf("%d", &input[i].num[j]);
		}
	}

	int count;
	for (int i = 1; i < n + 1; i++) {
		count = 0;
		for (int j = 0; j < SIZE; j++) {
			for (int k = 0; k < SIZE; k++) {
				if (input[i].num[j] == input[0].num[k]) count++;
			}
		}
		out[count - 1]++;
	}

	for (int j = SIZE-1; j >= 0; j--) {
		printf("%d ", out[j]);
	}	
	return 0;
}

在学习数据结构的时候,malloc分配内存,返回的指针类型在教材书里面都是强制转换为目标类型的,我的代码第12行加了就报错了,在vs里面是可以运行的,我把类型强制转换以后题目就通过了,这是为什么呢

2023/6/22 17:23
加载中...