C语言上机模拟试题1.docx
《C语言上机模拟试题1.docx》由会员分享,可在线阅读,更多相关《C语言上机模拟试题1.docx(19页珍藏版)》请在冰豆网上搜索。
C语言上机模拟试题1
C语言机试模拟试题
一、改错题:
(30分)
1、求一维数组a中的值为偶数的元素之和。
#include
sum(intarr[],intn)
{inti,s;
s=0;
for(i=0;iif(arr[i]%2==0)/************found************/s=s+i;改为:s=s+arr[i];return(s);}voidmain(){inta[10]={10,4,2,7,3,12,5,34,5,9},i,s;/************found************/s=sum(a,2);改为:s=sum(a,10);printf("Theresultis:%d\n",s);}2、求一维数组a中的值为奇数的元素之和。#include#includeintsum(intb[],intn){inti,s=0;for(i=0;iif(b[i]%2==1)/************found************/s=s+b[i]改为:s=s+b[i];return(s);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,19},n;/************found************/n=sum(a,2);改为:n=sum(a,12);printf("Theresultis:%d\n",n);}3、求二维数组a中的最大值和最小值。#includevoidmain(){inta[3][3]={4,4,34,37,3,12,5,6,5},i,j,max,min;//clrscr();max=min=a[0][0];for(i=0;i<3;i++)/************found************/for(j=1;j<3;j++)改为:for(j=0;j<3;j++){if(maxmax=a[i][j];/************found************/if(minif(min>a[i][j])min=a[i][j];}printf("Themaxis:%d\n",max);printf("Theminis:%d\n",min);}4、求一维数组a中的最大元素及其下标。#includevoidmain(){inta[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;//clrscr();max=a[0];pos=0;for(i=1;i<10;i++)/************found************/if(max>a[i])改为:if(max{max=a[i];/************found************/i=pos;改为:pos=i;}printf("Themaxis:%d,posis:%d\n",max,pos);}5、猴子吃桃的问题,求第一天的桃子数。#include#includevoidmain(){inti,j,x,p;//clrscr();x=1;/************found************/j=12;改为:j=11;while(j>=1){p=(x+1)*2;/************found************/p=x;改为:x=p;j--;}printf("totalis%d\n",p);}6、求一维数组a中的值为奇数的元素的平均值。#includedoubleaverage(intarr[],intn){intk=0,i;doubles;s=0;for(i=0;i/************found************/if(arr[i]%2=1)改为:if(arr[i]%2==1){s=s+arr[i];k++;}return(s/k);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,18};doubles;//clrscr();/************found************/s=average(a[12],12);改为:s=average(a,12);printf("Theresultis:%.2f\n",s);}二、填空题:(30分)1、程序的功能是:在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。程序如下:#defineM5voidmain(){inti,a[100],t;//clrscr();for(i=0;i/**************found************/___(1)___-填写的语句为:scanf(“%d”,&a[i]);/**************found************/for(i=0;___(2)___;i++)-填写的语句为:ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
if(arr[i]%2==0)
/************found************/
s=s+i;改为:
s=s+arr[i];
return(s);
}
voidmain()
{inta[10]={10,4,2,7,3,12,5,34,5,9},i,s;
s=sum(a,2);改为:
s=sum(a,10);
printf("Theresultis:
%d\n",s);
2、求一维数组a中的值为奇数的元素之和。
intsum(intb[],intn)
{inti,s=0;
for(i=0;iif(b[i]%2==1)/************found************/s=s+b[i]改为:s=s+b[i];return(s);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,19},n;/************found************/n=sum(a,2);改为:n=sum(a,12);printf("Theresultis:%d\n",n);}3、求二维数组a中的最大值和最小值。#includevoidmain(){inta[3][3]={4,4,34,37,3,12,5,6,5},i,j,max,min;//clrscr();max=min=a[0][0];for(i=0;i<3;i++)/************found************/for(j=1;j<3;j++)改为:for(j=0;j<3;j++){if(maxmax=a[i][j];/************found************/if(minif(min>a[i][j])min=a[i][j];}printf("Themaxis:%d\n",max);printf("Theminis:%d\n",min);}4、求一维数组a中的最大元素及其下标。#includevoidmain(){inta[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;//clrscr();max=a[0];pos=0;for(i=1;i<10;i++)/************found************/if(max>a[i])改为:if(max{max=a[i];/************found************/i=pos;改为:pos=i;}printf("Themaxis:%d,posis:%d\n",max,pos);}5、猴子吃桃的问题,求第一天的桃子数。#include#includevoidmain(){inti,j,x,p;//clrscr();x=1;/************found************/j=12;改为:j=11;while(j>=1){p=(x+1)*2;/************found************/p=x;改为:x=p;j--;}printf("totalis%d\n",p);}6、求一维数组a中的值为奇数的元素的平均值。#includedoubleaverage(intarr[],intn){intk=0,i;doubles;s=0;for(i=0;i/************found************/if(arr[i]%2=1)改为:if(arr[i]%2==1){s=s+arr[i];k++;}return(s/k);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,18};doubles;//clrscr();/************found************/s=average(a[12],12);改为:s=average(a,12);printf("Theresultis:%.2f\n",s);}二、填空题:(30分)1、程序的功能是:在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。程序如下:#defineM5voidmain(){inti,a[100],t;//clrscr();for(i=0;i/**************found************/___(1)___-填写的语句为:scanf(“%d”,&a[i]);/**************found************/for(i=0;___(2)___;i++)-填写的语句为:ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
if(b[i]%2==1)
s=s+b[i]改为:
s=s+b[i];
{inta[12]={10,4,2,7,3,12,5,34,5,9,21,19},n;
n=sum(a,2);改为:
n=sum(a,12);
%d\n",n);
3、求二维数组a中的最大值和最小值。
{inta[3][3]={4,4,34,37,3,12,5,6,5},i,j,max,min;
//clrscr();
max=min=a[0][0];
for(i=0;i<3;i++)
for(j=1;j<3;j++)改为:
for(j=0;j<3;j++)
{if(maxmax=a[i][j];/************found************/if(minif(min>a[i][j])min=a[i][j];}printf("Themaxis:%d\n",max);printf("Theminis:%d\n",min);}4、求一维数组a中的最大元素及其下标。#includevoidmain(){inta[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;//clrscr();max=a[0];pos=0;for(i=1;i<10;i++)/************found************/if(max>a[i])改为:if(max{max=a[i];/************found************/i=pos;改为:pos=i;}printf("Themaxis:%d,posis:%d\n",max,pos);}5、猴子吃桃的问题,求第一天的桃子数。#include#includevoidmain(){inti,j,x,p;//clrscr();x=1;/************found************/j=12;改为:j=11;while(j>=1){p=(x+1)*2;/************found************/p=x;改为:x=p;j--;}printf("totalis%d\n",p);}6、求一维数组a中的值为奇数的元素的平均值。#includedoubleaverage(intarr[],intn){intk=0,i;doubles;s=0;for(i=0;i/************found************/if(arr[i]%2=1)改为:if(arr[i]%2==1){s=s+arr[i];k++;}return(s/k);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,18};doubles;//clrscr();/************found************/s=average(a[12],12);改为:s=average(a,12);printf("Theresultis:%.2f\n",s);}二、填空题:(30分)1、程序的功能是:在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。程序如下:#defineM5voidmain(){inti,a[100],t;//clrscr();for(i=0;i/**************found************/___(1)___-填写的语句为:scanf(“%d”,&a[i]);/**************found************/for(i=0;___(2)___;i++)-填写的语句为:ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
max=a[i][j];
if(minif(min>a[i][j])min=a[i][j];}printf("Themaxis:%d\n",max);printf("Theminis:%d\n",min);}4、求一维数组a中的最大元素及其下标。#includevoidmain(){inta[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;//clrscr();max=a[0];pos=0;for(i=1;i<10;i++)/************found************/if(max>a[i])改为:if(max{max=a[i];/************found************/i=pos;改为:pos=i;}printf("Themaxis:%d,posis:%d\n",max,pos);}5、猴子吃桃的问题,求第一天的桃子数。#include#includevoidmain(){inti,j,x,p;//clrscr();x=1;/************found************/j=12;改为:j=11;while(j>=1){p=(x+1)*2;/************found************/p=x;改为:x=p;j--;}printf("totalis%d\n",p);}6、求一维数组a中的值为奇数的元素的平均值。#includedoubleaverage(intarr[],intn){intk=0,i;doubles;s=0;for(i=0;i/************found************/if(arr[i]%2=1)改为:if(arr[i]%2==1){s=s+arr[i];k++;}return(s/k);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,18};doubles;//clrscr();/************found************/s=average(a[12],12);改为:s=average(a,12);printf("Theresultis:%.2f\n",s);}二、填空题:(30分)1、程序的功能是:在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。程序如下:#defineM5voidmain(){inti,a[100],t;//clrscr();for(i=0;i/**************found************/___(1)___-填写的语句为:scanf(“%d”,&a[i]);/**************found************/for(i=0;___(2)___;i++)-填写的语句为:ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
if(min>a[i][j])
min=a[i][j];
printf("Themaxis:
%d\n",max);
printf("Theminis:
%d\n",min);
4、求一维数组a中的最大元素及其下标。
{inta[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;
max=a[0];
pos=0;
for(i=1;i<10;i++)
if(max>a[i])改为:
if(max{max=a[i];/************found************/i=pos;改为:pos=i;}printf("Themaxis:%d,posis:%d\n",max,pos);}5、猴子吃桃的问题,求第一天的桃子数。#include#includevoidmain(){inti,j,x,p;//clrscr();x=1;/************found************/j=12;改为:j=11;while(j>=1){p=(x+1)*2;/************found************/p=x;改为:x=p;j--;}printf("totalis%d\n",p);}6、求一维数组a中的值为奇数的元素的平均值。#includedoubleaverage(intarr[],intn){intk=0,i;doubles;s=0;for(i=0;i/************found************/if(arr[i]%2=1)改为:if(arr[i]%2==1){s=s+arr[i];k++;}return(s/k);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,18};doubles;//clrscr();/************found************/s=average(a[12],12);改为:s=average(a,12);printf("Theresultis:%.2f\n",s);}二、填空题:(30分)1、程序的功能是:在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。程序如下:#defineM5voidmain(){inti,a[100],t;//clrscr();for(i=0;i/**************found************/___(1)___-填写的语句为:scanf(“%d”,&a[i]);/**************found************/for(i=0;___(2)___;i++)-填写的语句为:ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
{
max=a[i];
i=pos;改为:
pos=i;
%d,posis:
%d\n",max,pos);
5、猴子吃桃的问题,求第一天的桃子数。
{inti,j,x,p;
x=1;
j=12;改为:
j=11;
while(j>=1)
{p=(x+1)*2;
p=x;改为:
x=p;
j--;
printf("totalis%d\n",p);
6、求一维数组a中的值为奇数的元素的平均值。
doubleaverage(intarr[],intn)
{intk=0,i;doubles;
for(i=0;i/************found************/if(arr[i]%2=1)改为:if(arr[i]%2==1){s=s+arr[i];k++;}return(s/k);}voidmain(){inta[12]={10,4,2,7,3,12,5,34,5,9,21,18};doubles;//clrscr();/************found************/s=average(a[12],12);改为:s=average(a,12);printf("Theresultis:%.2f\n",s);}二、填空题:(30分)1、程序的功能是:在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。程序如下:#defineM5voidmain(){inti,a[100],t;//clrscr();for(i=0;i/**************found************/___(1)___-填写的语句为:scanf(“%d”,&a[i]);/**************found************/for(i=0;___(2)___;i++)-填写的语句为:ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
if(arr[i]%2=1)改为:
if(arr[i]%2==1)
{s=s+arr[i];k++;}
return(s/k);
inta[12]={10,4,2,7,3,12,5,34,5,9,21,18};
doubles;
s=average(a[12],12);改为:
s=average(a,12);
%.2f\n",s);
二、填空题:
1、程序的功能是:
在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。
程序如下:
#defineM5
{inti,a[100],t;
for(i=0;i/**************found************/___(1)___-填写的语句为:scanf(“%d”,&a[i]);/**************found************/for(i=0;___(2)___;i++)-填写的语句为:ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
/**************found************/
___
(1)___-填写的语句为:
scanf(“%d”,&a[i]);
for(i=0;___
(2)___;i++)-填写的语句为:
ia[M-i-1]=a[i];for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
a[M-i-1]=a[i];
for(i=0;iprintf("%5d",a[i]);printf("\n");}2、计算正整数num的各位上的数字之和。#includevoidmain(){intnum,k;//clrscr();/************found************/___(1)___;填写的语句为:k=0;printf("Pleaseenteranumber:");scanf("%d",&num);do{/************found************/k=___(2)___;填写的语句为:k=k+num%10;num/=10;}while(num);printf("\n%d\n",k);}3、求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cmanis66#include#includelongintfun(intx){longints=1;inti;for(i=1;i<=x;i++)s*=i;/************found************/____(1)____;填写的语句为:returns;}voidmain(){longintm=12,n=2,s1,s2,s3;//clrscr();s1=fun(m);s2=fun(n);s3=fun(m-n);/************found************/printf("Cmnis:%ld",____(2)____);填写的语句为:s1/(s2*s3)}4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。5、#include"stdio.h"6、voidmain()7、{longintf[20]={1,1};8、inti;9、//clrscr();10、/**************found************/printf("%8ld%8ld",f[0],f[1]);11、for(i=2;i<20;i++)12、{13、f[i]=f[i-1]+f[i-2];14、/**************found************/if(i%5==0)15、printf("\n");16、printf("%8ld",f[i]);17、}18、printf("\n");19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。#include#includemain(){intm,n,i,t;longints=0;//clrscr();scanf("%d,%d",&m,&n);if(m>n){t=m;m=n;n=t;}/************found************/for(____(1)____;i++)填写的语句:i=m;i<=n;if(i%3==0&&i%7==0)s+=i;/************found************/printf("Sumis:____(2)____\n",s);填写的语句:%ld}20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。例如:从键盘输入12345,则程序运行的结果应当为:n=12345#include"stdio.h"#include"string.h"voidmain(){chars[10];inti;longintn;//clrscr();/**************found************/___(1)___;填写的语句:n=0scanf("%s",s);for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
printf("%5d",a[i]);
printf("\n");
2、计算正整数num的各位上的数字之和。
{intnum,k;
(1)___;填写的语句为:
k=0;
printf("Pleaseenteranumber:
");
scanf("%d",&num);
do
k=___
(2)___;填写的语句为:
k=k+num%10;
num/=10;
}while(num);
printf("\n%d\n",k);
3、求cman=m!
/n!
*(m-n)!
之值,例如:
m=12,n=2时,cmanis66
longintfun(intx)
{longints=1;inti;
for(i=1;i<=x;i++)
s*=i;
____
(1)____;填写的语句为:
returns;
{longintm=12,n=2,s1,s2,s3;
s1=fun(m);
s2=fun(n);
s3=fun(m-n);
printf("Cmnis:
%ld",____
(2)____);填写的语句为:
s1/(s2*s3)
4、输出Fabonacci数列1,1,2,3,5……前20项,要求每位输出5个数。
5、#include"stdio.h"
6、voidmain()
7、{longintf[20]={1,1};
8、inti;
9、//clrscr();
10、/**************found************/
printf("%8ld%8ld",f[0],f[1]);
11、for(i=2;i<20;i++)
12、{
13、f[i]=f[i-1]+f[i-2];
14、/**************found************/
if(i%5==0)
15、printf("\n");
16、printf("%8ld",f[i]);
17、}
18、printf("\n");
19、}求两个正整数[m,n]之间所有既能被3整除也能被7整除的整数之和。
main()
{intm,n,i,t;
longints=0;
scanf("%d,%d",&m,&n);
if(m>n)
{t=m;m=n;n=t;}
for(____
(1)____;i++)填写的语句:
i=m;i<=n;
if(i%3==0&&i%7==0)
s+=i;
printf("Sumis:
(2)____\n",s);填写的语句:
%ld
20、从键盘输入一个由数字组成的字符串(字符串长度小于8),将该字符串转换成一个十进制数。
例如:
从键盘输入12345,则程序运行的结果应当为:
n=12345
#include"stdio.h"
#include"string.h"
{chars[10];inti;
longintn;
(1)___;填写的语句:
n=0
scanf("%s",s);
for(i=0;i/**************found************/n=n*10+___(2)___;填写的语句:s[i]-48printf("n=%ld\n",n);}21、从键盘输入一个正整数n,计算并输出n的阶乘。#include#include#includelongfac(intn){inti;longt=1;for(i=2;i<=n;i++)t=t*i;return(t);}voidmain(){intn;longints;//clrscr();printf("Enteranintegernumber:");/************found************/____(1)____;填写的语句:scanf("%ld",&n);s=fac(n);/************found************/printf("Theresultis:____(2)____\n",s);填写的语句:%ld}三、编程题:(40分)1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(doublex){/***********begin***********/inty,i=1;doublea,sum=0.0;do{if(i%2==0)y=-1;elsey=1;a=y*1.0/(2*i-1);sum=sum+a;i++;}while(fabs(a)>=x);returnsum-a;/***********end***********/}voidmain(){doublex,sum;printf("Enterx:\n");scanf("%lf",&x);sum=fun(x);printf("sum=%.4f\n",sum);NONO();}NONO(){FILE*fr,*fw;inti;doublex;fr=fopen("D:\\exam\\03200310\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200310\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%lf",&x);fprintf(fw,"s=%.4f\n",fun(x));}fclose(fr);fclose(fw);}答案二:doubles=0;intn=1;while(x<=1.0/(2*n-1)){s+=pow(-1,n-1)*1.0/(2*n-1);n++;}returns;2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。如m=1234,输出的结果应为m=1234,4321.程序如下:(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=1234,a,b,c,d;/***********begin***********/a=m%10;b=m%100/10;c=m/100%10;d=m/1000;/************end************/printf("m=%d,%d%d%d%d\n",m,a,b,c,d);NONO(m,a,b,c,d);}NONO(x,a,b,c,d)intx,a,b,c,d;{FILE*f;f=fopen("D:\\exam\\03200302\\PROGOUT.DAT","w");fprintf(f,"x=%d:%3d%3d%3d%3d####\n",x,a,b,c,d);fclose(f);}3、按下面的公式,求S的近似值。S=1+1/2*2+1/3*3+…+1/n*n#include"stdio.h"#include"math.h"#include"stdlib.h"doublefun(intn){/***********begin***********/inti;doublep,sum=0.0;for(i=1;i<=n;i++){p=i*i;sum=sum+1.0/p;}returnsum;/************end************/}voidmain(){doubles;intn;//clrscr();printf("Enters:\n");scanf("%d",&n);s=fun(n);printf("s=%.3f\n",s);NONO();}NONO(){FILE*fr,*fw;inti,m;fr=fopen("D:\\exam\\03200306\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200306\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"s=%.3f####\n",fun(m));}fclose(fr);fclose(fw);}4、求一个三数m的各位上的数字之和。m由键盘输入。(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)#includeintfun(intm){/***********begin***********/inta,b,c;a=m/100;b=m/10%10;c=m%10;returna+b+c;/************end************/}voidmain(){intm,s;//clrscr();printf("Enterm:\n");scanf("%d",&m);s=fun(m);printf("Resultis:%d\n",s);NONO();}NONO(){FILE*fr,*fw;inti,j,k,m;fr=fopen("D:\\exam\\03200308\\PROGIN.DAT","r");fw=fopen("D:\\exam\\03200308\\PROGOUT.DAT","w");for(i=1;i<=5;i++){fscanf(fr,"%d",&m);fprintf(fw,"Sum=%d\n",fun(m));}fclose(fr);fclose(fw);}5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=76,n=40,t,i;/***********begin***********/for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=i;/************end************/printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,intt){FILE*f;f=fopen("D:\\exam\\03200309\\PROGOUT.DAT","w");fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);fclose(f);}6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********/temp=n*m;while(m!=0){i=n%m;n=m;m=i;}t=temp/n;/************end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);NONO(m,n,t);}NONO(intm,intn,ints){FILE*f;f=fopen("D:\\exam\\03200339\\PROGOUT.DAT","w");fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);fclose(f);7.编程:求两个整数m和n的最小公倍数,并将结果存入t中#include"stdio.h"#include"math.h"#include"stdlib.h"voidmain(){intm=6,n=45,t,i,temp;/***********begin***********//**********end************/printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);}答案:法一:for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
n=n*10+___
(2)___;填写的语句:
s[i]-48
printf("n=%ld\n",n);
21、从键盘输入一个正整数n,计算并输出n的阶乘。
longfac(intn)
{inti;
longt=1;
for(i=2;i<=n;i++)
t=t*i;
return(t);
{intn;
longints;
printf("Enteranintegernumber:
(1)____;填写的语句:
scanf("%ld",&n);
s=fac(n);
三、编程题:
(40分)
1、按下面的公式求s的近似值,直到最后一项值的绝对值小于x为止,x的值由键盘输入,结果保留4位小数。
S=1-1/3+1/5-1/9+…..+(-1)^(n-1)1/2n-1
#include"math.h"
#include"stdlib.h"
doublefun(doublex)
/***********begin***********/
inty,i=1;
doublea,sum=0.0;
do{
if(i%2==0)
y=-1;
elsey=1;
a=y*1.0/(2*i-1);
sum=sum+a;
i++;
}while(fabs(a)>=x);
returnsum-a;
/***********end***********/
doublex,sum;
printf("Enterx:
\n");
scanf("%lf",&x);
sum=fun(x);
printf("sum=%.4f\n",sum);
NONO();
NONO()
{FILE*fr,*fw;
inti;
doublex;
fr=fopen("D:
\\exam\\03200310\\PROGIN.DAT","r");
fw=fopen("D:
\\exam\\03200310\\PROGOUT.DAT","w");
for(i=1;i<=5;i++)
{fscanf(fr,"%lf",&x);
fprintf(fw,"s=%.4f\n",fun(x));}
fclose(fr);
fclose(fw);
答案二:
doubles=0;
intn=1;
while(x<=1.0/(2*n-1))
{s+=pow(-1,n-1)*1.0/(2*n-1);
n++;}
2、将一个4位的整数m拆开,然后按相反的顺序输出,每输出一位数其后留2个空格。
如m=1234,输出的结果应为m=1234,4321.程序如下:
(同学们自己编程,程序放在/***********begin***********/和/************end************/之间)
{intm=1234,a,b,c,d;
a=m%10;
b=m%100/10;
c=m/100%10;
d=m/1000;
/************end************/
printf("m=%d,%d%d%d%d\n",m,a,b,c,d);
NONO(m,a,b,c,d);
NONO(x,a,b,c,d)
intx,a,b,c,d;
{FILE*f;
f=fopen("D:
\\exam\\03200302\\PROGOUT.DAT","w");
fprintf(f,"x=%d:
%3d%3d%3d%3d####\n",x,a,b,c,d);
fclose(f);
3、按下面的公式,求S的近似值。
S=1+1/2*2+1/3*3+…+1/n*n
doublefun(intn)
doublep,sum=0.0;
for(i=1;i<=n;i++)
{p=i*i;
sum=sum+1.0/p;
returnsum;
{doubles;
intn;
printf("Enters:
scanf("%d",&n);
s=fun(n);
printf("s=%.3f\n",s);
inti,m;
\\exam\\03200306\\PROGIN.DAT","r");
\\exam\\03200306\\PROGOUT.DAT","w");
{fscanf(fr,"%d",&m);
fprintf(fw,"s=%.3f####\n",fun(m));}
4、求一个三数m的各位上的数字之和。
m由键盘输入。
intfun(intm)
inta,b,c;
a=m/100;
b=m/10%10;
c=m%10;
returna+b+c;
{intm,s;
printf("Enterm:
scanf("%d",&m);
s=fun(m);
printf("Resultis:
inti,j,k,m;
\\exam\\03200308\\PROGIN.DAT","r");
\\exam\\03200308\\PROGOUT.DAT","w");
fprintf(fw,"Sum=%d\n",fun(m));}
5、求两个正整数m和n的最大公约数,将求出的最大公数存入变量t中。
{intm=76,n=40,t,i;
for(i=n;i>0;i--)
if(m%i==0&&n%i==0)
break;
t=i;
printf("TheHighestCommonDivisorof%dand%dis%d\n",m,n,t);
NONO(m,n,t);
NONO(intm,intn,intt)
\\exam\\03200309\\PROGOUT.DAT","w");
fprintf(f,"MaximalCommonDivisorOf%dand%dis%d\n",m,n,t);
6、求两个正整数m和n的最小公倍数,将求出的最小公倍存入变量t中。
{intm=6,n=45,t,i,temp;
temp=n*m;
while(m!
=0)
{i=n%m;
n=m;
m=i;}
t=temp/n;
printf("TheLowestCommonMultipleof%dand%dis%d\n",m,n,t);
NONO(intm,intn,ints)
\\exam\\03200339\\PROGOUT.DAT","w");
fprintf(f,"MinimalCommonMultipleOf%dand%dis%d\n",m,n,s);
7.编程:
求两个整数m和n的最小公倍数,并将结果存入t中
/**********end************/
答案:
法一:
for(i=n;iif(i%n==0&&i%m==0){t=i;break;}法二:for(i=n;i>0;i--)if(m%i==0&&n%i==0)break;t=m*n/i;(这是先求最大公约数的方法,读者自行体会)8..程序目的是计算该数组的素数个数。。此运行结果是primenumber(s)is(are):6#include#includevoidmain(){inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;//clrscr();count=0;for(i=0;i<10;i++){k=sqrt(a[i]);for(j=2;j<=k;j++)if(a[i]%j==0)break;/************found************/if(____(1)____)count++;}/************found************/printf("primen
if(i%n==0&&i%m==0)
{t=i;
法二:
t=m*n/i;
(这是先求最大公约数的方法,读者自行体会)
8..程序目的是计算该数组的素数个数。
。
此运行结果是primenumber(s)is(are):
6
{inta[10]={2,3,5,7,8,9,10,11,12,13},i,j,k,count;
count=0;
for(i=0;i<10;i++)
k=sqrt(a[i]);
for(j=2;j<=k;j++)
if(a[i]%j==0)
if(____
(1)____)
count++;
printf("primen
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1