程序代码:#include<iostream>
#include<time.h>
#include<stdlib.h>
#define Size 10
using namespace std;
int *getx();
int *func(int*);
void put(int*);
int *getx()
{
static int a[Size];
srand(time(0));
unsigned int cou = 0;
for(unsigned int i = 0; i<Size; i++)
{
a[i] = rand() % 10;
if(a[i] == 0)++cou;
};
if(cou == 0)
{
int i = rand() % Size; a[i] = 0;
int j = rand() % Size;
while(i == j)
{
//rand() % Size;已经改为下面的
j=rand() % Size;
}
a[j] = 0;
}
if(cou == 1)
{
int i = rand() % Size;
while(a[i] == 0)
{
i = rand() % Size;
}
a[i] = 0;
}
for(unsigned int i = 0; i < Size; i++)cout << a[i] << "\t";
cout << endl;
return a;
}
int *func(int*p)
{
static int b[Size];
int cou = 0;
for(int i = 0, j = 0; i<Size; i++, j++)
{
if(*(p + i) != 0)
{
b[j] = *(p + i);
cou += 1;
}
else
{
j--;
}
}
for(int i = cou; i<Size; i++)b[i] = 0;
return b;
}
void put(int*p)
{
for(unsigned int i = 0; i < Size; i++)cout << *(p + i) << "\t";
cout << endl;
}
int main()
{
put(func(getx()));
system("pause");
return 0;
}
[此贴子已经被作者于2015-12-31 10:47编辑过]
程序代码:
#include<stdio.h>
int f(int * s,int idx,int len)
{
int i=0;
for(i=idx+1;i<len;i++)
{
if(*(s+i)>0)
{
return i;
break;
}
}
return len+9;
}
int main (void)
{
int s[8]={2,0,5,0,8,9,8,1};
int i=0;
for(;i<8;i++)
{
if(s[i]==0 && f(s,i,8)<8)
{
int t=s[f(s,i,8)];
s[f(s,i,8)]=s[i];
s[i]=t;
}
}
for(i=0;i<8;i++) printf("%4d",s[i]);
return 0;
}

[此贴子已经被作者于2015-12-31 10:56编辑过]