gpt4 book ai didi

c++ - 以下代码不会对 Stdout 做出任何响应

转载 作者:行者123 更新时间:2023-11-28 01:43:31 25 4
gpt4 key购买 nike

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

#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;
}

最佳答案

您的程序永远不会停止,因为您的最大查找循环对于 n > 0 是无限的。 birthdayCakeCandles 中的循环应更改为:

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

j++;
}

同时考虑使用更具可读性的编码风格,请阅读 this .

关于c++ - 以下代码不会对 Stdout 做出任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46179640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com