I don't know why, but the program is not entering into the FOR loop. I am completely new to programming so Please avoid any errors if there. Help much appreciated.
This question is from one of the coding websites::
You are given a list of N people who are attending ACM-ICPC World Finals. Each of them are either well versed in a topic or they are not. Find out the maximum number of topics a 2-people team can know. And also find out how many teams can know that maximum number of topics?
Input Format
The first line contains two integers N and M separated by a single space, where N represents the number of people, and M represents the number of topics. N lines follow. Each line contains a binary string of length M. In this string, 1 indicates that the ith person knows a particular topic, and 0 indicates that the ith person does not know the topic.
Output Format
On the first line, print the maximum number of topics a 2-people team can know. On the second line, print the number of teams that can know the maximum number of topics.
Constraints
2 ≤ N ≤ 500 1 ≤ M ≤ 500
Sample Input
4 5 10101 11100 11010 00101
Sample Output
5 2
Here is my code ::
#include<iostream>
using namespace std;
int main(){
int N,M;
cin>>N>>M;
if(N>=2 && N<=500 && M>=1 && M<=500){
int x= (N*(N-1))/2;
int i,j,k;
int Topic[x];
for(i=0;i<x;i++){
Topic[i]=0;
}
int y= N*M;
int a;
char Array[y];
while(N--){
for(i=0;i<M;i++){
cin>>Array[a];
a++;
}
}
int count;
int d=0;
int l=N-1;
// This FOR LOOP ..
**for(int p=0;p<l;p++){
for(int q=p+1;q<N;q++){
count=0;
for(k=0;k<M;k++){
int temp=k+(q*M);
int temp1=k+(p*M);
if(Array[temp]+Array[temp1]!=0){
count+=1;
}
}
Topic[d]=count;
d++;
}
}**
int max=Topic[0];
int counter=0;
for(i=0;i<x;i++){
if(max>Topic[i]){
max=Topic[i];
counter=1;
}
else if(Topic[i+1]=Topic[i]){
counter+=1;
}
}
cout<<max<<endl;
cout<<counter;
}
return 0;
}
You have a loop that counts N
down to 0
:
while (N--) {
// ...
}
Then you set l
to N - 1
, which is to say -1
:
int l = N - 1;
Then your for
loop wants to run while p < l
. p
is initially 0
, l
is initially -1
, so the loop never runs:
for ( int p = 0; p < l; p++ ) {
// ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.