Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 下面的代码没有对Stdout给出任何响应_C++_Arrays - Fatal编程技术网

C++ 下面的代码没有对Stdout给出任何响应

C++ 下面的代码没有对Stdout给出任何响应,c++,arrays,C++,Arrays,此代码用于计算数组中最大数的频率,即数组中出现最大数的次数。不幸的是,此代码不显示任何输出:- #include<iostream> #include <bits/stdc++.h> using namespace std; int birthdayCakeCandles(int n, int a[]){ int j=0,max,count=0; max = a[j]; while(j<n){ if(a[j+1]> max){ max = a[j+

此代码用于计算数组中最大数的频率,即数组中出现最大数的次数。不幸的是,此代码不显示任何输出:-

#include<iostream>
#include <bits/stdc++.h>

using namespace std;

int birthdayCakeCandles(int n, int a[]){
int j=0,max,count=0;
max = a[j];
while(j<n){
if(a[j+1]> max){
    max = a[j+1];
    j++;
}

}
int seen[n];
for(int i = 0; i < n; i++)
    seen[i] = 0; 

for(int i = 0; i < n;i++) {
    if(seen[i] == 0) {
        int count = 0;
        for(int j = i; j < n;j++)
            if(a[j] == a[i] && a[i] == max)
                count += 1;
                seen[j] = 1;

    }
    }
return count;
} 

int main() {
int i,n;
cin >> n;
int a[n];
for(i = 0; i < n; i++){
   cin >> a[i];
}
int result = birthdayCakeCandles(n, a);
cout << result << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int生日蜡烛(int n,int a[]){
int j=0,最大值,计数=0;
max=a[j];
while(j最大值){
最大值=a[j+1];
j++;
}
}
int-seen[n];
对于(int i=0;i>n;
int a[n];
对于(i=0;i>a[i];
}
int结果=生日蜡烛(n,a);

cout您的程序永远不会停止,因为您的最大查找循环用于
n>0
无止境。您在
生日蜡烛中的循环应更改为:

while (j < n)
{
    if (a[j + 1] > max)
    {
        max = a[j + 1];
    }

    j++;
}
while(j最大值)
{
最大值=a[j+1];
}
j++;
}

也考虑使用更可读的编码风格,请阅读.< /p> 您的程序从不停止,因为您的最大查找循环是针对<代码> n> 0 无穷无尽的。您的循环在<代码>生日KaCeCANDELs <代码>应改为:

while (j < n)
{
    if (a[j + 1] > max)
    {
        max = a[j + 1];
    }

    j++;
}
while(j最大值)
{
最大值=a[j+1];
}
j++;
}

还考虑使用更可读的编码风格,请阅读./P>< P>除了发现的bug之外,在至少复杂的后续循环中,您至少尝试了另一个错误,在这里尝试计算最大值的出现。

// I've kept OP's indentation on purpose... 
int seen[n];                 // <-- Variable Length Arrays are not standard in C++
for(int i = 0; i < n; i++)
    seen[i] = 0; 

for(int i = 0; i < n;i++) {
    if(seen[i] == 0) {
        int count = 0;
        for(int j = i; j < n;j++)
            if(a[j] == a[i] && a[i] == max)
                count += 1;
                seen[j] = 1;   // <-- misleading indentation, this is always executed
                               // no matter what the condition is 
    }
    }
//我故意保留OP的缩进。。。

int seen[n];//除了发现的错误之外,您在以下循环(过于复杂)中至少犯了另一个错误,您试图计算最大值的出现次数

// I've kept OP's indentation on purpose... 
int seen[n];                 // <-- Variable Length Arrays are not standard in C++
for(int i = 0; i < n; i++)
    seen[i] = 0; 

for(int i = 0; i < n;i++) {
    if(seen[i] == 0) {
        int count = 0;
        for(int j = i; j < n;j++)
            if(a[j] == a[i] && a[i] == max)
                count += 1;
                seen[j] = 1;   // <-- misleading indentation, this is always executed
                               // no matter what the condition is 
    }
    }
//我故意保留OP的缩进。。。

在C++标准中不允许看到[n];/ /可变长度数组。你应该是上面的代码>位/STDC++.H./Cult>页眉。远离所谓的竞争编程站点。@ Ron竞争编程本身是好的(人们可以并且确实做任何事情的竞争,这不是坏事)。。但是,这不是学习实用编程,甚至不是学习竞争编程的好方法。还请注意,如果n为5,当j为4时,它将执行[j+1],这与[5]相同因为数组是零索引的,这实际上是第六个元素,而在数组的末尾则是不可变的。在C++标准中不允许使用可变长度数组。您应该使用上面的代码> BITS/STDC++.H./Cult>页眉。远离所谓的竞争编程站点。(人们可以也确实可以用任何东西进行竞争,这不是一件坏事)。但是,这不是学习实用编程,甚至是竞争编程的好方法。还要注意,当你进行while(j max)时,你会阅读数组。例如,如果n是5,当j是4时,它会做[j+1],这与[5]相同,因为数组是零索引的,这实际上是第六个元素,超过了数组的末尾。谢谢,我没有注意到,因为不整洁的代码,现在我觉得很愚蠢。谢谢,我没有注意到,因为不整洁的代码,现在我觉得很愚蠢