C 不改变顺序,原址剔除数组中的0元素

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h> #define NUM_CNT 10000000
#define FILE_NAME "num.txt" void genNumber()
{
int i = ;
int *arr = (int*)malloc(sizeof(int) * NUM_CNT);
for(;i < NUM_CNT; ++i)
{
arr[i] = i;
}
FILE *fp = fopen(FILE_NAME,"w");
if(NULL == fp){printf("open %s failed\n",FILE_NAME);}
for(i = ;i < NUM_CNT; ++i)
{
if(i% == ){arr[i] = ;}
fprintf(fp,"%d ",arr[i]);
}
fclose(fp);
} void readNum(int arr[])
{
int i = ;
FILE *fp = fopen(FILE_NAME,"r");
if(NULL == fp){printf("open %s failed\n",FILE_NAME);}
for(i = ;i < NUM_CNT; ++i)
{
fscanf(fp,"%d ",&arr[i]);
}
fclose(fp);
} void dump(int *arr)
{
int i = ;
for(i = ;i < NUM_CNT; ++i)
{
printf("%d ",arr[i]);
}
printf("\n");
} void del(int *arr,int len)
{
int i = ,j = ,t = ;
for(; i < len - ; ++i)
{
if(arr[i] == )
{
j = i + ;
while(j < len && arr[j]==)++j;
if(j < len)
{
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
} void del1(int *arr,int len)
{
int i = ,j = ,t = ;
while(j < len && arr[j]==)++j;
if(j == len )return;
for(; i < len - && j < len; ++i)
{
if(arr[i] == )
{
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
while(j < len && arr[j]==)++j;
}
}
} int main()
{
int t = ;
int *arr= (int*)malloc(sizeof(int) * NUM_CNT);
genNumber();
readNum(arr);
//dump(arr);
t = clock();
del1(arr,NUM_CNT);
printf("use time:%lu ms\n",clock() - t);
//dump(arr);
return ;
}
上一篇:Java学习之路(七)


下一篇:mysql 5.1超过默认8小时空闲时间解决办法(错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure)