Pascal University, one of the oldest in the country, needs to renovate its Library Building, because after all these centuries the building started to show the effects of supporting the weight of the enormous amount of books it houses.
To help in the renovation, the Alumni Association of the University decided to organize a series of fund-raising dinners, for which all alumni were invited. These events proved to be a huge success and several were organized during the past year. (One of the reasons for the success of this initiative seems to be the fact that students that went through the Pascal system of education have fond memories of that time and would love to see a renovated Pascal Library.)
The organizers maintained a spreadsheet indicating which alumni participated in each dinner. Now they want your help to determine whether any alumnus or alumna took part in all of the dinners.
To help in the renovation, the Alumni Association of the University decided to organize a series of fund-raising dinners, for which all alumni were invited. These events proved to be a huge success and several were organized during the past year. (One of the reasons for the success of this initiative seems to be the fact that students that went through the Pascal system of education have fond memories of that time and would love to see a renovated Pascal Library.)
The organizers maintained a spreadsheet indicating which alumni participated in each dinner. Now they want your help to determine whether any alumnus or alumna took part in all of the dinners.
Input
The input contains several test cases. The first line of a test case contains two integers N and D indicating respectively the number of alumni and the number of dinners organized (1 <= N <= 100 and 1 <= D <= 500). Alumni are identified by integers from 1 to N. Each of the next D lines describes the attendees of a dinner, and contains N integers Xi indicating if the alumnus/alumna i attended that dinner (Xi = 1) or not (Xi = 0). The end of input is indicated by N = D = 0.
Output
For each test case in the input your program must produce one line of output, containing either the word `yes', in case there exists at least one alumnus/alumna that attended all dinners, or the word `no' otherwise.
Sample Input
3 3 1 1 1 0 1 1 1 1 1 7 2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0
Sample Output
yes no
Hint
Alumna: a former female student of a particular school, college or university.
Alumnus: a former male student of a particular school, college or university.
Alumni: former students of either sex of a particular school, college or university.
Alumnus: a former male student of a particular school, college or university.
Alumni: former students of either sex of a particular school, college or university.
题目的大意是:学校要重修图书馆,但是没钱,就举办了多次筹款晚宴,从校友那里捞钱,现在告述你举办筹款晚宴的次数、参加了筹款晚宴的校友的总人数、每场晚会哪些校友来了(0/1),让你判断有没有校友每场晚宴都来了,有,输出yes,没有,输出no。
这道题可以用一个二维数组num[][]来存储题目的输入,二维数组的行数就是举办筹款晚宴的次数,列数就是参加了筹款晚宴的校友的总人数,在题目输入完成后,只要将数组每一列中除了最后一行以外的所有数加到最后一行上,然后从最后一行的第一个数开始遍历,如果有任何一个数与筹款晚宴的次数相等,则输出yes,否则输出no。遍历完后使用free()函数释放分配给num的空间。
我发现在输出yes之后,既要不输出no又要结束循环的语句有点难写,我就使用了goto语句,直接跳到输出no的那条语句之后。
1 #include <stdio.h> 2 #include <malloc.h> 3 4 int main(void){ 5 int a,b,i,j; 6 int **num;//用来存储数据的二维数组 7 while(scanf("%d %d",&a,&b)==2){//a是列数,b是行数 8 if(a==0) break; 9 //根据a,b分配数组需要的空间 10 num=(int**)malloc(b*sizeof(int*)); 11 for(i=0;i<b;i++){ 12 num[i]=(int*)malloc(a*sizeof(int)); 13 } 14 //读取数据 15 for(i=0;i<b;i++){ 16 for(j=0;j<a;j++) scanf("%d",&num[i][j]); 17 } 18 //将数组每一列中除了最后一行以外的数加到最后一行上。 19 for(i=0;i<b-1;i++){ 20 for(j=0;j<a;j++) num[b-1][j]+=num[i][j]; 21 } 22 //从最后一行的第一个数开始遍历,如果这个数等于b,打印yes 23 for(i=0;i<a;i++){ 24 if(num[b-1][i]==b){ 25 printf("yes "); 26 goto m; 27 } 28 } 29 printf("no "); 30 //释放分配给num的内存空间 31 m: for(i=0;i<b;i++) free(num[i]); 32 free(num); 33 } 34 return 0; 35 }