请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例

Please find out the error in my code it is working for all my test cases still while submitting getting the wrong answer

本文关键字:错误 我的 答案 测试用例 适用于 提交 代码      更新时间:2023-10-16

这是问题的链接 https://www.codechef.com/JUNE20B/problems/CHFICRM 我写了两个不同的代码,根据我的说法,它们都工作正常,但仍然得到错误的答案,这是我的第二种方法。 https://www.codechef.com/JUNE20B/problems/CHFICRM 请有些人可以帮助我 ou.....

#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n;
k=5;
stack<int> s;
int b=1;
for(int i=0;i<n;i++)
{
int d;
cin>>d;
int y;
y=d-k;
if(y==0)
{
s.push(d);
continue;
}
if(s.empty() && d>5)
{
b--;
cout<<"NO"<<endl;
break;
}
while(!s.empty())
{
int z=y-s.top();
if(z==0)
{
s.pop();
s.push(d);
break;
}
else if(z>0){
s.pop();
if(s.empty())
{
cout<<"NO"<<endl;
b--;
break;
}
continue;
}
else if(z<0){
cout<<"NO"<<endl;
b--;
break;
}
}
if(b==0)
{
break;
}
}
if(b)
cout<<"YES"<<endl;
}
return 0;
}

在实践中,似乎需要一个简单的迭代过程。

在给定的时刻,只需计算可用的"5"硬币和"10"硬币的数量。

#include    <iostream>
#include    <vector>
int main() {
int t;
std::cin >> t;
while (t--) {
int n;
std::cin >> n;
std::vector<int> arr(n);
for (int i = 0; i < n; ++i) {
std::cin >> arr[i];
}
int c5 = 0, c10 = 0;
bool success = true;
for (int i = 0; (i < n) && success; ++i) {
switch (arr[i]) {
case(5):
c5++;
break;
case (10):
c10++;
c5--;
break;
case (15): 
if (c10) {
c10--;
} else {
c5 -= 2;
}
}
success = (c5 >= 0) && (c10 >= 0);
}
if (success) std::cout << "YESn";
else std::cout << "NOn";
}
}