【发布时间】:2021-12-12 01:54:47
【问题描述】:
我正在尝试从已排序的数组中删除重复项。代码为一个测试用例提供了正确的输出,但未能为多个测试用例提供正确的输出。我用其他方法得到了正确的输出,但这种方法有什么问题?我该如何解决这个问题?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n],i,k,temp,count;
for(i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
count=0;
for(i=0;i<n;i++){
if(a[i-1]-a[i]==0){
temp = a[i];
count++;
for(k=i;k<n;k++){
a[k] = a[k+1];
}
}
}
for(i=0;i<n-count;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
}
【问题讨论】:
-
我在 3 次罢工后停止阅读:
#include<bits/stdc++.h>然后using namespace std;最后是int a[n]。请考虑从介绍性 C++ 书籍开始,而不是使用hackerrank 或 leetcode 学习编码 -
我认为新的打击应该是那些发布代码的人,其输出依赖于 std::cin 的输入。基本上,如果您想从 SO 那里得到答案,则永远没有充分的理由发布使用 std::cin 的代码,除非您的问题与 std::cin 有关。
标签: c++ algorithm sorting for-loop duplicates