[英]Program is not entering into FOR loop
I don't know why, but the program is not entering into the FOR loop. 我不知道为什么,但是程序没有进入FOR循环。 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. 您会得到N个人参加ACM-ICPC世界总决赛的名单。 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. 找出2人团队可以知道的最大主题数。 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和M,它们之间用一个空格隔开,其中N表示人数,M表示主题数。 N lines follow. N行。 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. 每行包含一个长度为M的二进制字符串。在此字符串中,1表示第i个人知道特定的主题,0表示第i个人不知道该主题。
Output Format 输出格式
On the first line, print the maximum number of topics a 2-people team can know. 在第一行上,打印2人团队可以知道的最大主题数。 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
: 您有一个循环,将N
计数到0
:
while (N--) {
// ...
}
Then you set l
to N - 1
, which is to say -1
: 然后将l
设置为N - 1
,即-1
:
int l = N - 1;
Then your for
loop wants to run while p < l
. 然后,您的for
循环要在p < l
运行。 p
is initially 0
, l
is initially -1
, so the loop never runs: p
最初是0
, l
最初是-1
,所以循环永远不会运行:
for ( int p = 0; p < l; p++ ) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.