为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
Why I'm getting WA(Wrong Answer) in CMPRSS problem of codechef?
我在CodeChef的"压缩列表"问题代码(CMPRSS(中得到WA(错误答案(,这是问题的链接:https://www.codechef.com/problems/CMPRSS我已经检查了问题中给出的示例输出以及一些自制的测试用例,并且它工作正常。我没有发现我的代码中出了什么问题
这是我的方法:
#include <bits/stdc++.h>
using namespace std;
vector<int> number;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"n";
}
number.clear();
}
return 0;
}
您忘记检查零件中是否存在number[temp+1]
while (number[temp+1]-number[temp]==1){
temp++;
count++;
}
因此,它可能会读取数组之外并产生错误的输出.
请尝试这种情况:
2
5
1 2 3 4 5
3
1 2 3
部分应该是这样的:
while (temp+1 < static_cast<int>(number.size()) && number[temp+1]-number[temp]==1){
temp++;
count++;
}
你的逻辑部分正确,因为你忘了检查向量的上限索引: 请检查粗体字母中或代码之间缺少的代码:
#include 使用命名空间标准;
向量数; int main(( {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
number.push_back(temp);
}
int i;
for(i=0;i<n-1;i++){
int count = 0;
int temp = i;
while (**temp+1<n &&** number[temp+1]-number[temp]==1){
temp++;
count++;
}
if(count>1){
if(i+count==n-1){
cout<<number[i]<<"..."<<number[i+count]<<"n";
} else{
cout<<number[i]<<"..."<<number[i+count]<<",";
}
i = i + count;
}
else{
cout<<number[i]<<",";
}
}
if(i!=n){
cout<<number[n-1]<<"n";
}
number.clear();
}
return 0;
}
检查我的解决方案。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 为什么我在Codechef的褪色回文中得到一个WA,即使我在我的代码中没有发现任何错误,它对我来说工作得很好