当前位置:文档之家› 求出200——1000之间所有的素数

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

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 641643 647 653 659 661 673 677 683 691 701709 719 727 733 739 743 751 757 761 769773 787 797 809 811 821 823 827 829 839853 857 859 863 877 881 883 887 907 911919 929 937 941 947 953 967 971 977 983991 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 5135 72 67 4691 34 42 73 32 62 61 96 18 15 57 46 21 28 79 32 27 29 35 9493 61 12 13 43 84 31 71 93 38 87 24 92 97 67 89 93 31 89 6886 75 60 52 28 56 50 42 14 68 96 55 60 79 80 60 76 61 13 4819 33 64 74 46 60 26 96 81 38 34 49 66 83 57 38 18 22 79 8173 75 59 58 34 40 37 66 93 76 81 45 24 92 70 79 57 13 67 5256 70 81 16 45 27 85 14 81 92 90 80 31 44 26 20 67 51 17 9777 57 43 43 15 99 89 48 21 58 42 26 66 80 43 18 10 21 52 4550 69 94 27 48 13 65 91 12 60 41 66 24 50 16 91 88 79 58 1411 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->233->1 34->3 35->2 37->1 38->3 40->1 41->1 42->3 43->4 44->2 45->4 46->3 47->1 48->5 49->1 50->4 51->3 52->3 53->2 55->156->2 57->5 58->5 59->1 60->5 61->3 62->2 63->1 64->2 65->1 66->4 67->4 68->2 69->1 70->3 71->3 72->1 73->2 74->3 75->376->2 77->1 79->5 80->3 81->5 83->2 84->2 85->1 86->1 87->2 88->2 89->3 90->1 91->4 92->3 93->4 94->2 96->3 97->3 99->2Press 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->221->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->141->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->461->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->181->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->3Press any key to continue题目二使用函数指针完成数组排序,要求1)按冒泡选择法进行升序排列或者降序排列:2)程序包括sort,swap,ascending,descending,等函数。

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

相关主题