To show your intelligence to the Lord,you must solve the problem as follows:
Suppose there are m persons and n animals. A person can feed an animal peacefully if he/she is liked by the animal.
Given a list of which person is liked by which animal,you are to determine whether all animals can be feeded peacefully on
condition that every person can feed at most k animals.
输入格式
There will be multiple test cases. For each test case,the first line contains two integers m(1 <= m <= 100) and
n(1 <= n <= 100), where m indicates the number of persons and n indicated the number of animals.The following lines
contain the m * n 0-1 matrix indicating which person is liked by which animal.The ith person is liked by the jth animal
if the jth element of the ith line is 1. The last line for each test case contains a single integer k,indicating the
maximal number of animals one person can feed.
输出格式
For each test case,output "Yes" if all animals can be feeded peacefully,"No" otherwise.
样例输入
2 8
1 1 0 1 0 0 1 1
1 1 1 0 1 1 1 1
2
7 2
1 1
1 1
1 1
1 1
1 1
1 1
1 1
2
样例输出
No
Yes
得到数组a【m】【n】后,应该怎样遍历(或其他方法)数组以确认是否能和平地喂养。
个人理解:n必须是k的整数倍,一个人可以喂多次,但必须是一次喂k个动物,最后所有动物都有人喂。
创建了一个数组b【100】,选出那(n/k)个人(可重复),每人选k个,值置为1,直至全1则可以输出Yes。但是不知怎么实现
向大家请教请教思路方法,有代码更好
#include <stdio.h>
#include <string.h>
int main()
{
int m,n,k,i,j,num,sum=0;
int a[100][100];
int b[100];
while(scanf("%d %d", &m, &n) == 1)
{
for(i=0;i<m;i++) //m men n animals
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
if(n%k!=0) //
{ printf("No");continue;
}
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
sum += a[i][j];
if(sum==0) //no one can feed this animal
{ printf("No");continue;
}
... // b[100] !!!!!!!!!!!!!
}
memset(a,0,sizeof(a));
}
}