本帖最后由 jhq999 于 2021-11-9 20:40 编辑 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
int Rndary(int* inary,int arylen);
int Putary(int* inary,int arylen);
int Sortary(int* inary,int arylen);
int Delary(int* inary,int index,int *parylen);
int Rndary(int* inary,int arylen)
{
int i=0,j=0,rndval=0;
srand(unsigned int(time(NULL)));
for(i=0;i<arylen;i++)
{
rndval=rand()%301-100;
for(j=0;j<i;j++)
{
if(inary[j]==rndval)break;
}
if(j==i)inary[i]=rndval;
else
i--;
}
return 0;
}
int Putary(int* inary,int arylen)
{
int i=0;
for(i=0;i<arylen;i++)
{
printf("%d,",inary[i]);
}
return 0;
}
int Sortary(int* inary,int arylen)
{
int i=0,j=0,flag=1;
for(i=0;i<arylen;i++)
{
flag=1;
for(j=1;j<arylen-i;j++)
{
if(inary[j-1]>inary[j])
{
flag=0;
int tmp=inary[j-1];
inary[j-1]=inary[j];
inary[j]=tmp;
}
}
if(flag)break;
}
return 0;
}
int Delary(int* inary,int index,int *parylen)
{
int i=0;
for(i=index;i<*parylen;i++)//存疑如果是0开始的下标i<*parylen-1
{
inary[i-1]= inary[i];//inary[i]= inary[i+1];
}
*parylen--;
return 0;
}
int main()
{
int a[15]={0};
Rndary(a,15);
Sortary(a,15);
Putary(a,15);
int len=15;
Delary(a,10,&len);
Putary(a,len);
return 0;
}
|