C程序填空语言2744.docx
《C程序填空语言2744.docx》由会员分享,可在线阅读,更多相关《C程序填空语言2744.docx(17页珍藏版)》请在冰豆网上搜索。
C程序填空语言2744
27函数fun的功能是:
计算
的前n项之和。
若x=2.5,n=15时,函数值为:
1.917914
答案1。
1.02.(-1.0)3.t
解题关键:
1.设置初值(注意初值的类型【int?
double?
float?
】),与循环结合起来分析初值2.对于自乘运算看前后两项的关系3.注意返回值
#include
#include
doublefun(doublex,intn)
{doublef,t;inti;
/**********found**********/
f=___1___;
t=-1;
for(i=1;i{
/**********found**********/
t*=(___2___)*x/i;
/**********found**********/
f+=___3___;
}
returnf;
}
main()
{doublex,y;
x=2.5;
y=fun(x,15);
printf("\nTheresultis:
\n");
printf("x=%-12.6fy=%-12.6f\n",x,y);
}
28函数fun的功能是:
计算
直到
<
。
若x=2.5,函数值为1.917915
答案:
1.x2.n3.fabs(t)
#include
#include
doublefun(doublex)
{doublef,t;intn;
f=1.0+x;
/**********found**********/
t=___1___;
n=1;
do{
n++;
/**********found**********/
t*=(-1.0)*x/___2___;
f+=t;
}
/**********found**********/
while(___3___>=1e-6);
returnf;
}
main()
{doublex,y;
x=2.5;
y=fun(x);
printf("\nTheresultis:
\n");
printf("x=%-12.6fy=%-12.6f\n",x,y);
}
29.
函数fun的功能是:
计算
n=12时。
若x=2.5,函数值为12.182340
1.1.02.13.i
#include
doublefun(doublex,intn)
{doublef,t;inti;
f=1.0;
/**********found**********/
t=___1___;
/**********found**********/
for(i=___2___;i{
/**********found**********/
t*=x/___3___;
f+=t;
}
returnf;
}
main()
{doublex,y;
x=2.5;
y=fun(x,12);
printf("\nTheresultis:
\n");
printf("x=%-12.6fy=%-12.6f\n",x,y);
}
30.
函数fun的功能是:
计算
直到
<
。
若x=2.5,函数值为12.182494
1.x2。
n3.t
#include
#include
doublefun(doublex)
{doublef,t;intn;
/**********found**********/
f=1.0+___1___;
t=x;
n=1;
do{
n++;
/**********found**********/
t*=x/___2___;
/**********found**********/
f+=___3___;
}while(fabs(t)>=1e-6);
returnf;
}
main()
{doublex,y;
x=2.5;
y=fun(x);
printf("\nTheresultis:
\n");
printf("x=%-12.6fy=%-12.6f\n",x,y);
}
31.函数fun的功能是:
统计所有小于等于n(n>2)的素数的个数,素数的个数作为函数值返回。
答案:
1.j=22.i3.j
核心为素数的判断算法
#include
intfun(intn)
{inti,j,count=0;
printf("\nTheprimenumberbetween3to%d\n",n);
for(i=3;i<=n;i++){
/**********found**********/
for(___1___;j
/**********found**********/
if(___2___%j==0)
break;
/**********found**********/
if(___3___>=i)
{count++;printf(count%15?
"%5d":
"\n%5d",i);}
}
returncount;
}
main()
{intn=20,r;
r=fun(n);
printf("\nThenumberofprimeis:
%d\n",r);
}
32
函数fun的功能是:
统计长整数n的各个位上出现数字1,2,3的次数,并通过外部(局部)变量c1,c2,c3返回主函数。
例如:
当n=123114350时,结果应该为:
c1=3,c2=1,c3=2
答案:
1.n%102.break3.break
核心为取余运算(%)与除法运算(/)配合使用。
#include
intc1,c2,c3;
voidfun(longn)
{c1=c2=c3=0;
while(n){
/**********found**********/
switch(___1___)
{
/**********found**********/
case1:
c1++;___2___;
/**********found**********/
case2:
c2++;___3___;
case3:
c3++;
}
n/=10;
}
}
main()
{longn=123114350L;
fun(n);
printf("\nTheresult:
\n");
printf("n=%ldc1=%dc2=%dc3=%d\n",n,c1,c2,c3);
}
33.
用筛选法可等到2~n(n<10000)之间的所有素数,方法是:
首先从素数2开始,将所有2的倍数从数表中删除(把数表中相应位置的值置成0);接着从数表中找下一个非0数,并从数表中删去该数所有的倍数;依次类推,直到所找的下一个数等于n为止。
这样会得到一个序列:
2,3,5,7,11,13,17,......
答案:
1.a[i],2a[i]3.0
#include
intfun(intn)
{inta[10000],i,j,count=0;
for(i=2;i<=n;i++)a[i]=i;
i=2;
while(i/**********found**********/
for(j=a[i]*2;j<=n;j+=___1___)
a[j]=0;
i++;
/**********found**********/
while(___2___==0)
i++;
}
printf("\nTheprimenumberbetween2to%d\n",n);
for(i=2;i<=n;i++)
/**********found**********/
if(a[i]!
=___3___)
{count++;printf(count%15?
"%5d":
"\n%5d",a[i]);}
returncount;
}
main()
{intn=20,r;
r=fun(n);
printf("\nThenumberofprimeis:
%d\n",r);
}
34.甲乙丙丁四人同时开始放鞭炮,甲每隔t1秒放一次,已每隔t2秒放一次,丙每隔t3秒放一次,丁每隔t4秒放一次,每人各放n次。
函数fun的功能是根据形参提供的值,求出总共听到多少次鞭炮声作为函数值返回。
注意,当及格鞭炮同时炸响,只算一次响声,第一次响声是在0秒。
例如,若t1=7,t2=5,t3=6,t4=4,n=10,则总共可听到28次鞭炮。
答案:
1.i2.t++3.count(不用学C语言也可以做)
#include
/**********found**********/
#defineOK(i,t,n)((___1___%t==0)&&(i/tintfun(intt1,intt2,intt3,intt4,intn)
{intcount,t,maxt=t1;
if(maxtif(maxtif(maxtcount=1;/*给count赋初值*/
/**********found**********/
for(t=1;t{
if(OK(t,t1,n)||OK(t,t2,n)||OK(t,t3,n)||OK(t,t4,n))
count++;
}
/**********found**********/
return___3___;
}
main()
{intt1=7,t2=5,t3=6,t4=4,n=10,r;
r=fun(t1,t2,t3,t4,n);
printf("Thesound:
%d\n",r);
}
35.
函数fun的功能是:
从3个形参a,b,c中找出中间的那个数,作为函数值返回。
例如:
当a=3,b=5,c=4,中数为4。
答案:
1.a,2a3.b
核心为三条件语句的使用,方法是层层运算,脱掉括号
#include
intfun(inta,intb,intc)
{
intt;
/**********found**********/
t=(a>b)?
(b>c?
b:
(a>c?
c:
___1___)):
((a>c)?
___2___:
((b>c)?
c:
___3___));
returnt;
}
main()
{inta1=3,a2=5,a3=4,r;
r=fun(a1,a2,a3);
printf("\nThemiddlenumberis:
%d\n",r);
}
36.
给定程序中,函数fun的功能是:
找出100至x(x<=999)之间各位上的数字之和为15的所有整数,然后输出;符合条件的整数个数作为函数值返回。
例如,当n值为500时,各位数字之和为15的整数有:
159,168,177,186,195,249......共有26个
答案:
1.0;2.x3.t++
#include
intfun(intx)
{intn,s1,s2,s3,t;
/**********found**********/
n=__1__;
t=100;
/**********found**********/
while(t<=__2__)
{s1=t%10;s2=(t/10)%10;s3=t/100;
if(s1+s2+s3==15)
{printf("%d",t);
n++;
}
/**********found**********/
__3__;
}
returnn;
}
main()
{intx=-1;
while(x>999||x<0)
{printf("Pleaseinput(0");scanf("%d",&x);}
printf("\nTheresultis:
%d\n",fun(x));
}
37
给定程序中,函数fun的功能是:
找出100~999之间(含100和999)所有整数各位上数字之和为x(x为以正整数)的整数,然后输出;符合条件的整数个数作为函数值返回。
例如:
当x值为5时,100~999之间各位上数字之和为5的整数有:
104,113,122,131,140,203,212,221,230,302,311,320,401,410,500。
共有15个。
当x值为27时,各位数字之和为27的整数是:
999。
只有1个。
答案;1.9992.t/103.x
#include
intfun(intx)
{intn,s1,s2,s3,t;
n=0;
t=100;
/**********found**********/
while(t<=__1__){
/**********found**********/
s1=t%10;s2=(__2__)%10;s3=t/100;
/**********found**********/
if(s1+s2+s3==__3__)
{printf("%d",t);
n++;
}
t++;
}
returnn;
}
main()
{intx=-1;
while(x<0)
{printf("Pleaseinput(x>0):
");scanf("%d",&x);}
printf("\nTheresultis:
%d\n",fun(x));
}
38.给定程序中,函数fun的功能是:
将形参n中,各位上为偶数的数取出,并按原来从高位到低位的顺序组成一个新的数,并作为函数值返回。
例如:
从主函数输入一个整数,27638496,函数返回值为:
26846
答案:
1.12.s>03.i*10
#include
unsignedlongfun(unsignedlongn)
{unsignedlongx=0,s,i;intt;
s=n;
/**********found**********/
i=__1__;
/**********found**********/
while(__2__)
{t=s%10;
if(t%2==0){
/**********found**********/
x=x+t*i;i=__3__;
}
s=s/10;
}
returnx;
}
main()
{unsignedlongn=-1;
while(n>99999999||n<0)
{printf("Pleaseinput(0");scanf("%ld",&n);}
printf("\nTheresultis:
%ld\n",fun(n));
}
39.给定程序中,函数fun的功能是:
将形参n中,各位上为偶数的数取出,并按原来从高位到低位相反的顺序组成一个新的数,并作为函数值返回
例如:
输入一个整数:
27638496,函数返回值为:
64862
答案;
1.02.10*x3.n/10
#include
unsignedlongfun(unsignedlongn)
{unsignedlongx=0;intt;
while(n)
{t=n%10;
/**********found**********/
if(t%2==__1__)
/**********found**********/
x=__2__+t;
/**********found**********/
n=__3__;
}
returnx;
}
main()
{unsignedlongn=-1;
while(n>99999999||n<0)
{printf("Pleaseinput(0");scanf("%ld",&n);}
printf("\nTheresultis:
%ld\n",fun(n));
}
40.给定程序中,函数fun的功能是:
将形参n所指变量中,各位上为偶数的数去除,剩下的数按原来从高位到低位的顺序组成一个新的数,并通过形参指针n传回所指变量。
例如:
输入一个数:
27638496,新的数:
739.
答案:
1.102.03.x
#include
voidfun(unsignedlong*n)
{unsignedlongx=0,i;intt;
i=1;
while(*n)
/**********found**********/
{t=*n%__1__;
/**********found**********/
if(t%2!
=__2__)
{x=x+t*i;i=i*10;}
*n=*n/10;
}
/**********found**********/
*n=__3__;
}
main()
{unsignedlongn=-1;
while(n>99999999||n<0)
{printf("Pleaseinput(0");scanf("%ld",&n);}
fun(&n);
printf("\nTheresultis:
%ld\n",n);
}
41.
答案:
1.0.02.n3(4*i*i)
注意初值和循环体及返回值
#include
doublefun(intn)
{inti;doubles,t;
/**********found**********/
s=__1__;
/**********found**********/
for(i=1;i<=__2__;i++)
{t=2.0*i;
/**********found**********/
s=s+(2.0*i-1)*(2.0*i+1)/__3__;
}
returns;
}
main()
{intn=-1;
while(n<0)
{printf("Pleaseinput(n>0):
");scanf("%d",&n);}
printf("\nTheresultis:
%f\n",fun(n));
}
42.
答案:
1.12.2*i3.(-1)
#include
doublefun(intn)
{inti,k;doubles,t;
s=0;
/**********found**********/
k=__1__;
for(i=1;i<=n;i++){
/**********found**********/
t=__2__;
s=s+k*(2*i-1)*(2*i+1)/(t*t);
/**********found**********/
k=k*__3__;
}
returns;
}
main()
{intn=-1;
while(n<0)
{printf("Pleaseinput(n>0):
");scanf("%d",&n);}
printf("\nTheresultis:
%f\n",fun(n));
}
43.
答案:
1.3.02.>=3.t+1
#include
doublefun(doublee)
{inti,k;doubles,t,x;
s=0;k=1;i=2;
/**********found**********/
x=__1__/4;
/**********found**********/
while(x__2__e)
{s=s+k*x;
k=k*(-1);
t=2*i;
/**********found**********/
x=__3__/(t*t);
i++;
}
returns;
}
main()
{doublee=1e-3;
printf("\nTheresultis:
%f\n",fun(e));
}
44.
答案:
1.02.i++3.2.0*i
#include
doublefun(doublee)
{inti;doubles,x;
/**********found**********/
s=0;i=__1__;
x=1.0;
while(x>e){
/**********found**********/
__2__;
/**********found**********/
x=(2.0*i-1)/((__3__)*(2.0*i));
s=s+x;
}
returns;
}
main()
{doublee=1e-3;
printf("\nTheresultis:
%f\n",fun(e));
}