ccf 点亮人生C语言源代码

这个只能拿八十分,好像是LOOP那最大值的时候爆掉了但是不知道哪爆了有没有大佬指点一下

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct object {
	int kind;
	int indegree;
	bool select;
	int output1;
	int output2;
	int input1;
	int input2;
	int result;
} object;

int **graph;
int **input;
object *objects;
int inputnumber;
int objectnumber;
int s;
bool equal(char *a, char *b) {
	int n = strlen(b);
	for (int i = 0; i < n; ++i) {
		if (a[i] != b[i]) return false;
	}
	return true;
}


void init() {
	scanf("%d %d", &inputnumber, &objectnumber);
	graph = (int**)malloc(sizeof(int*) * (objectnumber + 1));
	for (int i = 1; i <= objectnumber; ++i) {
		graph[i] = (int*)malloc(sizeof(int) * (objectnumber + 1));
		for (int j = 1; j <= objectnumber; ++j) {
			graph[i][j] = 0;
		}
	}
	objects = (object*)malloc(sizeof(object) * (objectnumber + 1));
	for (int i = 1; i <= objectnumber; ++i) {
		objects[i].indegree = 0;
		objects[i].input1 = 0;
		objects[i].input2 = 0;
		objects[i].kind = 0;
		objects[i].output1 = 0;
		objects[i].output2 = 0;
		objects[i].result = 0;
		objects[i].select = false;
	}
	for (int i = 1; i <= objectnumber; ++i) {
		char kind[10];
		int inputnum;
		scanf("%s %d ", &kind, &inputnum);
		if (inputnum == 1) {
			char iokind;
			int objnumber;
			scanf("%c%d", &iokind, &objnumber);
			if (iokind == 'O') {
				graph[objnumber][i] = 1;
				objects[i].indegree++;
				objects[i].output1 = objnumber;
			} else {
				objects[i].input1 = objnumber;
			}
			objects[i].kind = 1;
		} else {
			char iokind1, iokind2;
			int objnumber1, objnumber2;
			scanf("%c%d %c%d", &iokind1, &objnumber1, &iokind2, &objnumber2);
			if (iokind1 == 'O') {
				++objects[i].indegree;
				graph[objnumber1][i] = 1;
				objects[i].output1 = objnumber1;
				if (iokind2 == 'O') {
					++objects[i].indegree;
					graph[objnumber2][i] = 1;
					objects[i].output2 = objnumber2;
				} else {
					objects[i].input1 = objnumber2;
				}
			} else {
				objects[i].input1 = objnumber1;
				if (iokind2 == 'O') {
					++objects[i].indegree;
					graph[objnumber2][i] = 1;
					objects[i].output1 = objnumber2;
				} else {
					objects[i].input2 = objnumber2;
				}
			}
			char b[4] = "AND";
			if (equal(kind, b)) objects[i].kind = 2;
			char c[3] = "OR";
			if (equal(kind, c)) objects[i].kind = 3;
			char d[] = "XOR";
			if (equal(kind, d)) objects[i].kind = 4;
			char e[] = "NAND";
			if (equal(kind, e)) objects[i].kind = 5;
			char f[] = "NOR";
			if (equal(kind, f)) objects[i].kind = 6;
		}	
	}
}

int outnumber(int n, int x) {
	if (objects[x].kind == 1) {
		int temp;
		if (objects[x].input1) {
			temp = input[n][objects[x].input1];
		} else {
			temp = objects[objects[x].output1].result;
		}
		objects[x].result =!temp;
	}
	int temp1, temp2;
	if (objects[x].input1) {
		temp1 = input[n][objects[x].input1];
		if (objects[x].output1) {
			temp2 = objects[objects[x].output1].result;
		} else {
			temp2 = input[n][objects[x].input2];
		}
	} else {
		temp1 = objects[objects[x].output1].result;
		if (objects[x].input1) {
			temp2 = input[n][objects[x].input1];
		} else {
			temp2 = objects[objects[x].output2].result;
		}
	}
	if (objects[x].kind == 2) {
		objects[x].result = temp1 & temp2;
	}
	if (objects[x].kind == 3) {
		objects[x].result = temp1 | temp2;
	}
	if (objects[x].kind == 4) {
		objects[x].result = temp1 ^ temp2;
	}
	if (objects[x].kind == 5) {
		objects[x].result = !(temp1 & temp2);
	}
	if (objects[x].kind == 6) {
		objects[x].result = !(temp1 | temp2);
	}
	return objects[x].result;
}

bool topu() {
	for (int i = 1; i <= objectnumber; ++i) {
		int sign = 0;
		for (int j = 1; j <= objectnumber; ++j) {
			if (!objects[j].select && objects[j].indegree == 0) {
				for (int k = 1; k <= objectnumber; ++k) {
					if (graph[j][k]) {
						graph[j][k] = 0;
						objects[k].indegree--;
					}
				}
				objects[j].select = i;
				sign = 1;
				break;
			}
		}
		if (sign == 0) return true;
	}
	return false;
}

void f(int n) {
	for (int i = 1; i <= objectnumber; ++i) {
		for (int j = 1; j <= objectnumber; ++j) {
			if (objects[j].select == i) {
				outnumber(n, j);
			} 
		} 
	}
}



void answer() {
	int ans_top = 0;
	scanf("%d", &s);
	int sign = 0;
	if (topu()) {
		getchar();
		for (int i = 1; i <= s * 2; ++i) {
			char *a = (char*)malloc(sizeof(char) * 2000);
			gets(a);
		}
		printf("LOOP\n");
		return;
	}
	int ans[20005];
	input = (int**)malloc(sizeof(int*) * (s + 1));
	for (int i = 1; i <= s; ++i) {
		input[i] = (int*)malloc(sizeof(int) * (inputnumber + 1));
	}
	for (int i = 1; i <= s; ++i) {
		for (int j = 1; j <= inputnumber; ++j) {
			scanf("%d", &input[i][j]);
		}
	}
	for (int i = 1; i <= s; ++i) {
		f(i);
		int t;
		scanf("%d", &t);
		ans[ans_top++] = t;
		for (int j = 0; j < t; ++j) {
			int t1;
			scanf("%d", &t1);
			ans[ans_top++] = objects[t1].result;
		}
	}
	int r = 0;
	while (r < ans_top) {
		int temp = ans[r++];
		for (int i = 1; i <= temp && r < ans_top; ++i) {
			printf("%d", ans[r++]);
			printf(" ");
		}
		printf("\n");
	}
}
int main() {
	int Q;
	scanf("%d", &Q);
	for (int p = 0; p < Q; ++p) {
		init();
		answer();
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值