【ACM】杭电1144:Prerequisites


我觉得这个题要说有难度的话,那唯一的难度就是数据构成比较复杂,要用合理的结构来存储输入的数据。

我用了2个结构体:

存储课程的号码:

typedef struct
{
	char id[10];
}CLASSID;

存储每一个分类下的课程信息:

typedef struct /* 存放每个分类的课程信息 */
{
	int min_take;  /* 至少要上的课数 */ 
	int amount_class; /* 这个分类下的课程总数 */
	CLASSID class_name[110];
}CATEGORY;

完整代码:

#include <stdio.h>
#include <string.h>

typedef struct
{
	char id[10];
}CLASSID;

typedef struct /* 存放每个分类的课程信息 */
{
	int min_take;  /* 至少要上的课数 */ 
	int amount_class; /* 这个分类下的课程总数 */
	CLASSID class_name[110];
}CATEGORY;

void take_lesson(CLASSID *choose,CATEGORY *cy,int lesson,int category) /* 模拟上课过程 */
{
	int i,j,k;
	for(i = 1 ; i <= lesson ; ++i)
	{
		for(j = 1 ; j <= category ; ++j)
		{
			for(k = 1 ; k <= cy[j].amount_class ; ++k)
			{
				if(!strcmp(cy[j].class_name[k].id,choose[i].id))
					--cy[j].min_take;
			}
		}
	}
}

int is_finish(CATEGORY *cy,int category) /* 判断是否能完成 */
{
	int i;
	for(i = 1 ; i <= category ; ++i)
	{
		if(cy[i].min_take > 0)
		{
			return 0;
		}
	}
	return 1;
}

int main(int argc, char *argv[])
{
	int lesson,category;
	while(scanf("%d",&lesson) != EOF && lesson != 0)
	{
		scanf("%d",&category);
		int t,tt;
		CLASSID class_choose[110];
		CATEGORY cy[110];
		
		memset(class_choose,0,sizeof(class_choose));
		memset(cy,0,sizeof(cy));
		
		for(t = 1 ; t <= lesson ; ++t) /* 输入选择的课程号 */
			scanf("%s",class_choose[t].id);
		
		for(t = 1 ; t <= category ; ++t) /* 每个分类 */
		{	
			/* 输入每个分类下的课程数量,和这个类别至少上的课数 */
			scanf("%d%d",&cy[t].amount_class,&cy[t].min_take);
			
			for(tt = 1 ; tt <= cy[t].amount_class ; ++tt) /* 用来输入每个分类下的 课程号 */
				scanf("%s",cy[t].class_name[tt].id);
		}
		
		take_lesson(class_choose,cy,lesson,category); /* 上课 */
		if(is_finish(cy,category))
			printf("yes\n");
		else
			printf("no\n");
		
	}
	return 0;
}


Problem Description
Freddie the frosh has chosen to take k courses. To meet the degree requirements, he must take courses from each of several categories. Can you assure Freddie that he will graduate, based on his course selection? 
 

Input
Input consists of several test cases. For each case, the first line of input contains 1 ≤ k ≤ 100, the number of courses Freddie has chosen, and 0 ≤ m ≤ 100, the number of categories. One or more lines follow containing k 4-digit integers follow; each is the number of a course selected by Freddie. Each category is represented by a line containing 1 ≤ c ≤ 100, the number of courses in the category, 0 ≤ r ≤ c, the minimum number of courses from the category that must be taken, and the c course numbers in the category. Each course number is a 4-digit integer. The same course may fulfil several category requirements. Freddie's selections, and the course numbers in any particular category, are distinct. A line containing 0 follows the last test case.
 

Output
For each test case, output a line containing "yes" if Freddie's course selection meets the degree requirements; otherwise output "no." 
 

Sample Input
   
   
   
   
3 2 0123 9876 2222 2 1 8888 2222 3 2 9876 2222 7654 3 2 0123 9876 2222 2 2 8888 2222 3 2 7654 9876 2222 0
 

Sample Output
   
   
   
   
yes no
 

Source
University of Waterloo Local Contest 2005.09.24




你可能感兴趣的:(【ACM】杭电1144:Prerequisites)