题解 | #序列中整数去重#

序列中整数去重

https://www.nowcoder.com/practice/6564a2f6e70f4153ad1ffd58b2b28490

#include <iostream>

using namespace std;

int main() {
    int n;
    cin>>n;
    int a[1000],b[1000];
    for(int i=0;i<n;i++){
      cin>>a[i];
      b[i]=a[i];
    }
    for(int j=0;j<n;j++){
      int num=0;
      for(int m=0;m<n;m++){
         if(a[j]==b[m]){
            num++;
            if(num>=2){
               b[m]=0;
            }
         }
      }
      
    }

    for(int o=0;o<n;o++){
         if(b[o]!=0){
            cout<<b[o]<<" ";
         }
    }
}

两个数组,b复制a数组,用a数组中的每一个数去对比b数组中的数,用num记录出现次数,当出现次数大于等于2时,说明重复出现,将对应位置的数变成0,最后输出所有不为0的数字;

全部评论

相关推荐

理智的芭乐在查重:这边男朋友还有hc吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务