求出200——1000之间所有的素数

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C语言程序设计
综合实验报告
学院:信息科学与工程学院
专业:自动化1002班
学号:201004134070 姓名:吴君
指导老师:
2011年6月25日武汉科技大学
求出200——1000之间所有的素数,要求1)调用函数判断某数是不是素数;
2)输出结果,每行输出十个;
程序:
#include<stdio.h>
#include<math.h>
int judge(int n)//定义一个函数
{int i,k;
k=sqrt(n);
for(i=2;i<=k;i++)//判断I是否是素数{
if(n%i==0)
{ break;
}
}
if (i>k)
{
return 1;//返回一个函数值
}
return 0;
}
void main()
{int i,m,k;
for(i=201;i<1000;i=i+2)
{
m=judge(i);//调用自定义函数
if (m==1)
{
printf("%4d",i); //输出结果
k++;
if(k%10==0)//大于10换行
printf("\n");
}
}
}
输出结果:
211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641
643 647 653 659 661 673 677 683 691 701
709 719 727 733 739 743 751 757 761 769
773 787 797 809 811 821 823 827 829 839
853 857 859 863 877 881 883 887 907 911
919 929 937 941 947 953 967 971 977 983
991 997Press any key to continue
利用随机函数产生200个正整数,统计这200个正整数中相同的个数。

要求
1)用标准函数编程并输出;
2)利用标准函数和结构体编程输出结果;
程序:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{int array[200]={0},num[100]={0};
int i,j, k,h,b;
printf("得到的两位数是:");
for(i=0;i<200;i++) //获得200个两位数
{
array[i]=rand()%90+10;
printf("%3d",array[i]);h++;
if(h%20==0)
{
printf("\n"); }
}
for(j=0;j<200;j++) //统计两位数的个数 {
num[array[j]]++;
}
printf("统计结果为:");
for(k=10;k<100;k++)
if(num[k]!=0)
{
printf("%4d->%d",k,num[k]); //输出结果b++;
if(b%20==0)
{printf("\n");}
}
}
其结果为:
得到的两位数是: 51 27 44 50 99 74 58 28 62 84 45 75 71 97 71 51
35 72 67 46
91 34 42 73 32 62 61 96 18 15 57 46 21 28 79 32 27 29 35 94
93 61 12 13 43 84 31 71 93 38 87 24 92 97 67 89 93 31 89 68
86 75 60 52 28 56 50 42 14 68 96 55 60 79 80 60 76 61 13 48
19 33 64 74 46 60 26 96 81 38 34 49 66 83 57 38 18 22 79 81
73 75 59 58 34 40 37 66 93 76 81 45 24 92 70 79 57 13 67 52
56 70 81 16 45 27 85 14 81 92 90 80 31 44 26 20 67 51 17 97
77 57 43 43 15 99 89 48 21 58 42 26 66 80 43 18 10 21 52 45
50 69 94 27 48 13 65 91 12 60 41 66 24 50 16 91 88 79 58 14
11 64 63 29 58 48 70 18 47 27 48 53 88 83 87 91 10 57 53 74
统计结果为: 10->2 11->1 12->2 13->4 14->3 15->2 16->2 17->1 18->4 19-
>1 20->1 21->3 22->1 24->3 26->3 27->5 28->3 29->2 31->3 32->2
33->1 34->3 35->2 37->1 38->3 40->1 41->1 42->3 43->4 44->2 45->4 4
6->3 47->1 48->5 49->1 50->4 51->3 52->3 53->2 55->1
56->2 57->5 58->5 59->1 60->5 61->3 62->2 63->1 64->2 65->1 66->4 6
7->4 68->2 69->1 70->3 71->3 72->1 73->2 74->3 75->3
76->2 77->1 79->5 80->3 81->5 83->2 84->2 85->1 86->1 87->2 88->2 8
9->3 90->1 91->4 92->3 93->4 94->2 96->3 97->3 99->2
Press any key to continue
其结构体程序:
#include <stdio.h>
#include <stdlib.h>
#include <TIME.H>
struct Array
{
int arr[200];
};
int main(int argc, char* argv[])
{
srand( (unsigned)time( NULL ) );
Array arrStr;
// int arr[200] = {0}; //保存产生的两位数int num[90] = {0}; //记录重复出现次数int temp = 0;
//产生随机数
for (int i =0; i < 200; i ++)
{
temp = rand()%90 + 10;
arrStr.arr[i] = temp;
}
//统计出现次数
for (int j = 0; j < 200; j ++)
{
num[ arrStr.arr[j] - 10 ] ++;
}
//打印结果
printf("每个数出现的次数统计为:\n");
for (int k =0; k < 90; k++)
{
if (num[k] > 0)
{printf("%2d->%d ", k+10, num[k]);
if((k+10)%20==0)
{printf("\n");}
}
}
return 0;
}
结果是:
每个数出现的次数统计为:
10->5 11->2 12->2 13->3 14->3 15->3 16->3 17->3 18->2 20->2
21->3 23->5 24->1 25->2 26->1 27->3 28->1 29->2 30->1 31->2 32->3 33->4 34->2 35
->1 37->4 38->1 39->2 40->1
41->6 42->3 43->3 44->1 45->2 46->1 47->3 48->3 49->4 50->3 51->1 52->3 53->4 54
->1 55->2 56->1 57->2 58->3 59->1 60->4
61->2 64->3 65->1 66->4 67->2 68->2 69->1 70->1 71->3 72->1 73->2 74->4 76->3 77
->3 78->4 79->3 80->1
81->3 82->2 83->3 84->1 86->2 87->2 88->1 89->4 90->1 91->1 92->2 93->2 94->3 95
->3 96->3 97->2 98->4 99->3
Press any key to continue
题目二
使用函数指针完成数组排序,要求
1)按冒泡选择法进行升序排列或者降序排列:
2)程序包括sort,swap,ascending,descending,等函数。

其中sort接受ascending或descending函数指针参数、
一个整形数组合数组长度。

3)函数原形提供如下:
Void sort(int[],const int ,int(*)(int,int));
int ascending(int,int);
int descending(int,int);
4)具有提示使用者选择升序或者降序排列功能。

如果使用者输入1,则函数sort传递ascending函数指针,数组按升序排列输出,输入2则函数sort传递descending函数指针,数组将会按降序排列并输出。

程序:
#include<stdio.h>
#include<stdlib.h>
int ascending(int a,int b)//定义函数;作升序用
{
return(a<b);
}
int descending(int a,int b)//定义函数;作降序用
{
return(a>b);
}
void sort(int a[],const int n,int(*p)(int,int))//定义排序函数,其中用函数指针传递排序方式
{
int i,j=0, temp = 0;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(p(a[j],a[i]))
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}//end for(j...)
}//for(i...)
}
void main()
{
int arr[10] = {0};
int i = 0;
int flag= 0; //标志位,排序选择,若为1则升序排序,若为2降序
printf("请输入 10 个数进行排序:\n");
for(i=0;i<10;i++) //循环输入数组的元素值
{
scanf("%d",&arr[i]);
}
printf("请输入排序方式(1 升序;2 降序):");
scanf("%d",&flag); //输入数字选择用那种排序(升或降)
if(flag==1)
{
sort(arr, 10, ascending);
printf("升序排序后:\n");
for(i=0; i<10; i++)
{
printf("%3d",arr[i]); //输出排序后的数组}
}
if(flag==2)
{
sort(arr, 10, descending);
printf("降序排序后:\n");
for(i=0;i<10;i++)
{
printf("%4d",arr[i]);
}
}
printf("\n");
}
结果:
升序为:请输入 10 个数进行排序:
15 45 25 35 85 95 65 55 75 54
请输入排序方式(1 升序;2 降序):1
升序排序后:
15 25 35 45 54 55 65 75 85 95
Press any key to continue
降序为:
请输入 10 个数进行排序:
11 25 32 10 54 26 31 12 16 91
请输入排序方式(1 升序;2 降序):2
降序排序后:
91 54 32 31 26 25 16 12 11 10 Press any key to continue。

相关文档
最新文档