【FOJ】Problem 1354 Forests

Problem 1354 Forests.

题意

  • How many different opinions are represented in the input?
    Two people hold the same opinion only if they hear exactly the same set of trees.
  • 输入:
    人数P,树的数目T(均小于100)
    随后若干行i、j,表示第 i 个人听到了第 j 颗树倒下
    i=j=0结束输入
  • 输出:
    问:关于“哪些树倒下了有几种不同的观点?”
    输出不同观点的数量

思路

  • 二维数组opinion[][],opinion[i]表示i的观点,opinion[i][j]=1表示第i个人的观点里树j倒下了
  • 总观点数=人数,遍历opinion,出现相同的观点则观点数ans-1,并做标记,不重复检测这个已经相同的观点

代码

#include<cstdio>
#include<string.h>
using namespace std;

int opinion[100][100];
int flag[100];

int main(){
	int p, t, i, j, ans;
	while(scanf("%d%d", &p, &t)!=EOF){
		memset(opinion, 0, sizeof(opinion));
		memset(flag, 0, sizeof(flag));
		scanf("%d%d", &i, &j);
		while(i!=0 && j!=0){
			opinion[i][j] = 1;
			scanf("%d%d", &i, &j);
		}
		ans = p;
		int temp;	//辅助判断i的观点和j的观点是否相同
		for(i=1; i<p; i++){	//将i与他后面的人的观点做比较
			if(flag[i])	//如果i在前面的检测中已经与其他观点相同,不重复检测i
				continue;
			for(j=i+1; j<=p; j++){
				temp = 0;
				for(int k=1; k<=t; k++){
					if(opinion[i][k]!=opinion[j][k]){
						temp = 1;
						break;
					}
				}
				if(temp==0){	//表示i,j观点一致
					ans--;
					flag[j] = 1;
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}
发布了46 篇原创文章 · 获赞 0 · 访问量 461

猜你喜欢

转载自blog.csdn.net/qq_44531167/article/details/105372360