资源描述:
《C语言考前辅导14-15(2).doc》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、C语言考前辅导1、求输入的整数中正数的个数及其平均值#includemain(){intnum=0;floatsum=0,a;while(scanf("%f",&a)!=EOF){if(a<=0.0)continue;num++;sum+=a;}printf("%dplusinteger'ssum:%6.0f",num,sum);printf("Meanvalue:%6.2f",sum/num);}2、求1/a+1/aa+1/aaa+…的前5项之和。(若a=3,则是1/3+1/33+1/333+1/3333+1/33333之和)#de
2、fineN5main(){inta,k;floatt,s;scanf(“%d”,&a);s=1.0/a;t=a;for(k=2;k<=N;k++){t=t*10;t=t+a;s=s+1.0/t;}printf(“1/a+1/aa+1/aaa+…=%f”,s);}如果改成求a+aa+aaa+aaaa+aaaaa如何编程三s=a;t=a;{t=10*t+aS=s+t}3、打印100~200之间的所有素数#include"math.h"main(){intm,k,i,n=0;for(m=101;m<=200;m=m+2){if(n%10==0)printf(""
3、);k=sqrt(m);for(i=2;i<=k;i++)if(m%i==0)break;if(i>=k+1) {printf("%d",m); n++;}}}if(i>k)比较好理解4、求n的阶乘,1)递归#includeintfac(intn){intf;if(n<0)printf("n<0,dataerror!");elseif(n==0
4、
5、n==1)f=1;elsef=fac(n-1)*n;return(f);}main(){intn,y;printf("Inputaintegernumber:");scanf("%d",&n);
6、y=fac(n);printf("%d!=%15d",n,y);}2)非递归main(){floaty;Intn;printf("Inputaintegernumber:");scanf("%d",&n);y=fac(n);printf("%d!=%15f",n,y);}floatfact(intn){inti;doubleres=1;for(i=1;i<=n;i++)res=res*i;returnres;}5、读10个整数存入数组,找出其中最大值和最小值#include#defineSIZE10main(){intx[SIZE],i,ma
7、x,min;printf("Enter10integers:");for(i=0;ix[i])min=x[i];}printf("Maximumvalueis%d",max);printf("Minimumvalueis%d",min);}6、冒泡排序算法分析#includemain(){inta[11],i,j,t
8、;printf("Input10numbers:");for(i=1;i<11;i++)scanf("%d",&a[i]);printf("");for(j=1;j<=9;j++)for(i=1;i<=10-j;i++)if(a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}printf("Thesortednumbers:");for(i=1;i<11;i++)printf("%d",a[i]);}8.11写一函数用起泡法对输入的个字符按由小到大的顺序排列。intpaixu(x)intx[];{inti,j,t;
9、for(j=1;j<10;j++)for(i=0;i<=9-j;i++)if(x[i]>x[i+1]){t=x[i+1];x[i+1]=x[i];x[i]=t;}}main(){inty[10];inti;for(i=0;i<10;i++)scanf("%d",&y[i]);paixu(y);for(i=0;i<10;i++)printf("%5d",y[i]);printf("");7、用简单选择法对10个数排序 用选择法对10个整数进行排序。 首先,我们要明白什么是选择法,选择法就是首先将n个数中最小的数与a[0]对换,再将a[1]到a[n-1]中
10、最小的数与a[1]对换,每比较一轮,找