11.17完成对无数组中重复数据的剔除

11.17完成对无序数组中重复数据的剔除

#include <stdio.h>
/*produce some numbers not bigger than ten 
and change the content to The simple one*/
#include<time.h>
#define N 20
int main()
{
 int N1[N];//define an array 
 int i1 = 0, i2 = 0, i4 = 0, j = 0;//define the control variable.
 
 for (i1 = 0; i1 < N; i1++)/*Input the local array*/
  N1[i1] = rand() % 10;
 for (i1 = 0; i1 < N; i1++)/*Output the localarray*/
  printf("%d\t", N1[i1]);
 putchar('\n');
 for (i1 = 0, i2 = 0; i1 < N - i2;++i1)/*Change the array to 
              fit the demand*/
 {
  i4 = i1 + 1;
  for (; i4 < N - i2; ++i4)/*Delete the repeated ones*/
   if (N1[i1] == N1[i4])/*Tell the difference and change it.*/
   {
    for (j = i4; j < N - i2 ; j++)
     N1[j] = N1[j + 1];
    ++i2;
    --i4;
   }
 }
 for (j = 0; j <N-i2; ++j)/*Output the new array.*/
  printf("%d\t", N1[j]);
 return 0;
}
 在这个过程中,最重要的就是对于重复数据的剔除。

 那么,对于这个剔除的筛选阶段,我们就进行“控制数”筛选:也就是 使用i2对我们读取操作的控制。
 
  再看,最终的剔除阶段:我们的i4作为比较数,其中的变化应当考虑到:也就是考虑第一次使用和之后进行循环的时候,有什么不一样的地方。
  显然:我们在剔除之后,对i4进行了“+1“的操作,但是if语句中的判断位置应该是不发生变化的,
  所以,在if语句最后,我们对i4进行了”-1“操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值