#include "stdio.h"
#include
#include "time.h"
void outprime(int a[]){
int x[50],i,j,k,t;
for(i=0;i<50;i++){//找素数并存入x
printf("%4d",a[i]);
if((a[i]&1)==0)
continue;
for(t=0,k=3;k*k<=a[i];k+=2)
if(a[i]%k==0)
break;
if(k*k>a[i])
x[t++]=a[i];
}
printf("\n");
for(i=0;ifor(k=i,j=k+1;j if(x[k]>x[j])
k=j;
if(k-i)
j=x[k],x[k]=x[i],x[i]=j;
printf("%4d",x[i]);
}
printf(t ? "\n" : "No...\n");
}
int main(int argv,char *argc[]){
int i,a[50];
printf("50 random numbers from 100 to 200\n");
srand((unsigned)time(NULL));//下伪随机种子
for(i=0;i<50;i++)
a[i]=rand()%100+100;
outprime(a);
return 0;
}
运行结果:
#include
#include
#include"time.h"
int prime(int a[],int n)
{
int i,j,b[50],m=0,k,*p;
for(i=0;i{
k=0;
for(j=2;j {
if(a[i]%j==0)
k=1;
}
if(k==0)
{
b[m]=a[i];
m++;
}
}
for(i=0;i{
a[i]=b[i];
}
return m+1;
}
void h(int a[],int n)
{
int i,j,t;
for(i=0;ifor(j=0;j if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
void main()
{
srand(time(NULL));
int i,n=50,a[50],*b;
printf("50 random numbers from 100 to 200\n");
for(i=0;i<50;i++)
{
a[i]=rand()%100+100;
}
for(i=0;i<50;i++)
printf("%d ",a[i]);
printf("\n素数排序:\n");
n=prime(a,n);
h(a,n);
for(i=0;iprintf("%d ",a[i]);
printf("\n");
}
我在你原有程序修改了一下
#include
#include
#include
int prime(int n) {
int i,f = 1;
for(i = 2;i < n/2;i++)
if(n%i == 0) f = 0;
return f;
}
void sort(int a[],int n) {
int i,j,t;
for(i = 0;i < n - 1;i++) {
for(j = i + 1;j < n;j++) {
if(a[j] < a[i]) {
t = a[j];
a[i] = a[j];
a[j] = t;
}
}
}
}
int main() {
int i,j = 0,a[50],p[50];
printf("50 random numbers from 100 to 200\n");
srand((unsigned)(time(NULL)));
for(i = 0;i < 50;i++) {
a[i] = rand()%100 + 100;
if(prime(a[i])) p[j++] = a[i];
}
sort(p,j);
for(i = 0; i < j; ++i) printf("%d ",p[i]);
printf("\n");
return 0;
}
#include
#include
#include
#include
int prime(int n)
{
int i;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
int main()
{
srand((unsigned)time(0));
int a[100];
int i;
for(i=0;i<100;i++)
{
a[i]=0;
}
int count=0,n,find;
while(count<50)
{
find=0;
n=rand()%99+101;
for(i=0;i<100;i++)
{
if(a[i]==n)
{
find=1;
break;
}
}
if(find==0)
{
a[n-100]=n;
if(count%10==0)
{
printf("\n");
}
printf("%d ",n);
count++;
}
}
printf("\n素数从小到大:\n");
for(i=0;i<100;i++)
{
if(a[i]!=0 && prime(a[i])==1)
printf("%d ",a[i]);
}
return 0;
}