[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。
你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,去掉所有重复的数字。最后按照输入顺序输出没有重复数字的数据。所有重复的数字只保留第一次出现的那份。
输入格式:
你的程序首先会读到一个正整数 n,1≤n≤100000。
然后是 n 个整数,这些整数的范围是 [1, 100000]。
输出格式:
在一行中按照输入顺序输出去除重复之后的数据。每两个数据之间有一个空格,行首尾不得有多余空格。
方法一:
#include<stdio.h> int main() { int n,i=1; scanf("%d",&n); int k,judge[100001]={
0};//这里的k取代了NUM【】数组 scanf("%d",&k); printf("%d",k); judge[k]++; while(i<n) {
scanf("%d",&k); if(judge[k]==0) { printf(" %d",k); judge[k]++; } i++; } printf(
"\n"); }
方法二:
#include<stdio.h> int main() { int n,i=1; scanf("%d",&n); int num[n],judge[
100001]={0}; scanf("%d",&num[0]); printf("%d",num[0]); judge[num[0]]++; while(i<
n) { scanf("%d",&num[i]); if(judge[num[i]]==0) { printf(" %d",num[i]); judge[num
[i]]++; } i++; } printf("\n"); }
方法三:
#include <stdio.h> int main() { int n,i,k,j,q; int a[100000]; //定义一个10000长数组
scanf("%d",&n); //输入m for(i=0;i<n;i++) { scanf("%d",&a[i]); //循环输入n个数 getchar();
//以空格隔开 } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[j]==a[i])
//如果第i个数和i+1以后的数比较,相等则赋值-1 { a[j]=-1; } } } for(q=0;q<n;q++) //循环输出去重后的结果 { if (
a[q]>0) { printf("%d",a[q]); if(q<n-1) putchar(' '); } } }