gpt4 book ai didi

c++ - Google Kickstart 2020问题记录破坏者错误答案

转载 作者:行者123 更新时间:2023-12-01 14:51:38 24 4
gpt4 key购买 nike

我正在练习上一轮Google Kick Start 2020提出的问题。这个问题称为Record Breaker,如下所示:

Isyana is given the number of visitors at her local theme park on Nconsecutive days. The number of visitors on the i-th day is Vi. A dayis record breaking if it satisfies both of the following conditions:The number of visitors on the day is strictly larger than the numberof visitors on each of the previous days. Either it is the last day,or the number of visitors on the day is strictly larger than thenumber of visitors on the following day. Note that the very first daycould be a record breaking day!

Please help Isyana find out the number of record breaking days.

Input The first line of the input gives the number of test cases, T. Ttest cases follow. Each test case begins with a line containing theinteger N. The second line contains N integers. The i-th integer isVi.

Output For each test case, output one line containing Case #x: y,where x is the test case number (starting from 1) and y is the numberof record breaking days.

Limits Time limit: 20 seconds per test set. Memory limit: 1GB. 1 ≤ T ≤100. 0 ≤ Vi ≤ 2 × 105.

Test set 1 1 ≤ N ≤ 1000.

Test set 2 1 ≤ N ≤ 2 × 105 for at most 10 test cases. For theremaining cases, 1 ≤ N ≤ 1000.

Sample

Input 4 8 1 2 0 7 2 0 2 0 6 4 8 15 16 23 42 9 3 1 4 1 5 9 2 6 5 6 9 99 9 9 9

Output Case #1: 2 Case #2: 1 Case #3: 3 Case #4: 0

In Sample Case #1, the bold and underlined numbers in the followingrepresent the record breaking days: 1 2 0 7 2 0 2 0.

In Sample Case #2, only the last day is a record breaking day.

In Sample Case #3, the first, the third, and the sixth days are recordbreaking days.

In Sample Case #4, there is no record breaking day.


这是我创建的解决方案。它在第一个测试用例中给出了错误的答案,但我想不出我错过的任何特定情况。
#include<iostream>
#include<algorithm>

using namespace std;

int record_breaking(long long int arr[], long long int n)
{
long long int ans = 0;
long long int m = arr[0];
for(long long int i = 0; i<n; i++)
{
//For first element
if(i == 0 && arr[0] > arr[1])
{
ans++;
}

//For the last element
else if(i == n - 1 && arr[i] > m)
{
ans++;
//cout<<arr[i]<<" is a answer "<<endl;

}
//Normal Case
else if(arr[i] > m && arr[i] > arr[i + 1])
{
ans++;
//cout<<arr[i]<<" is a answer "<<endl;
}
m = max(arr[i], m);
}
return ans;
}

int main()
{
int t;
cin>>t;
for(int test = 1; test <= t; test++)
{
long long int n;
cin>>n;
long long int arr[200000];
//int *arr = new int [n];
for(long long int i = 0; i<n; i++)
{
cin>>arr[i];
}
cout<<endl<<"Case #"<<test<<": "<<record_breaking(arr, n);
//delete [] arr;
}

return 0;
}
请帮助我找出解决方案的问题!

最佳答案

这会过去...
边缘保护套:
输入:
1个
1个
2
输出:
1个
但是您的代码给出了0作为输出...
怎么了:
您首先要添加if语句为(i == 0 && arr [0]> arr [1]),但是元素个数不超过...因此,这里首先您必须检查这种情况(i == n- 1 && arr [i]> m)...这意味着您必须进行正确的订购...
另外,对于上述情况,将m = -1 Crucial初始化。

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int record_breaking(vector<int> arr, int n)
{
int ans = 0;
int m = -1;
for(int i = 0; i<n; i++)
{

//For the last element
if(i == n - 1 && arr[i] > m)
{
ans++;
//cout<<arr[i]<<" is a answer "<<endl;

}

//For first element
else if(i == 0 && arr[0] > arr[1])
{
ans++;
}

//Normal Case
else if(arr[i] > m && arr[i] > arr[i + 1])
{
ans++;
//cout<<arr[i]<<" is a answer "<<endl;
}
m = max(arr[i], m);
}
return ans;
}

int main()
{
int t;
cin>>t;
for(int test = 1; test <= t; test++)
{
int n, temp;
cin>>n;
vector<int> arr;
//long long int arr[200000];
//int *arr = new int [n];
for(int i = 0; i<n; i++)
{
cin>>temp;
arr.push_back(temp);
}
cout<<endl<<"Case #"<<test<<": "<<record_breaking(arr, n);
//delete [] arr;
}

return 0;
}

关于c++ - Google Kickstart 2020问题记录破坏者错误答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63190624/

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