份二C上机编程题答案.docx
- 文档编号:9708328
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:86
- 大小:39.29KB
份二C上机编程题答案.docx
《份二C上机编程题答案.docx》由会员分享,可在线阅读,更多相关《份二C上机编程题答案.docx(86页珍藏版)》请在冰豆网上搜索。
份二C上机编程题答案
本答案仅供参考(请不要随意更改)
A类数学表达式(1—10)(N)
1、
#include
#include
floatfun(intm,intn)
{
floatt1=1,t2=1,t3=1,i;
for(i=1;i<=m;i++)
t1*=i;
for(i=1;i<=n;i++)
t2*=i;
for(i=1;i<=m-n;i++)
t3*=i;
returnt1/(t2*t3);
}
main()
{
printf("P=%f\n",fun(12,8));
}
2
#include
#include
#include
floatfun()
{
floatx1=0.0,x0;
x0=x1;
x1=cos(x0);
while(fabs(x0-x1)>=0.000001)
{
x0=x1;
x1=cos(x0);
}
returnx1;
}
main()
{
printf("Root=%f\n",fun());
}
3
#include
#include
doublefun(intn)
{
doublef0=0,f1=1,fn;
fn=f0+f1;
while(fn<=n)
{
f0=f1;
f1=fn;
fn=f0+f1;
}
returnfn;
}
main()/*主函数*/
{
printf("%f\n",fun(1000));
}
4
#include
#include
voidfun(float*sn,intn)
{
floati,t=1;
*sn=0;
for(i=0;i<=n;i++)
{*sn+=t/(2*i+1);
t=-t;
}
}
main()/*主函数*/
{intn=11;floats;
fun(&s,n);
printf("s=%fn=%d\n",s,n);
}
5
#include
#include
#include
doublefun(intm)
{
doubles=0,i;
for(i=1;i<=m;i++)
s+=log(i);
returnsqrt(s);
}
main()
{
printf("%f\n",fun(20));
}
6
#include
#include
#include
doublefun(doublex,intn)
{
doubles=x+1,i,t=1;
for(i=2;i<=n;i++)
{
t*=i;
s+=pow(x,i)/t;
}
returns;
}
main()
{
printf("%f\n",fun(0.3,10));
}
7
#include
#include
doublefun(intn)
{
doubles=0,i;
for(i=1;i<=n;i++)
s+=1.0/(i*(i+1));
returns;
}
main()
{
printf("%f",fun(10));
}
8
#include
#include
doublefun(doubleeps)
{
doubles=0,t=1,i,m=1;
for(i=1;t/m>=eps;i++)
{
s+=t/m;
t=t*i;
m=m*(2*i+1);
}
return2*s;
}
main()
{doublex;
printf("Inputeps:
");
scanf("%lf",&x);printf("\neps=%lf,PI=%lf\n",x,fun(x));
}
9
#include
#include
doublefun(doublex)
{
doublei,t=1,m=1,s=0;
for(i=1;fabs(m/t)>=0.000001;i++)
{
s=s+m/t;
m*=(0.5+1-i)*x;
t=t*i;
}
returns;
}
main()
{doublex,s;
printf("Inputx:
");scanf("%lf",&x);
s=fun(x);
printf("s=%f\n",s);
}
10
#include
#include
doublefun(doublex[10])
{
doubles=0,sum=0;
inti;
for(i=0;i<10;i++)
s=s+x[i];
s=s/10;
for(i=0;i<10;i++)
sum+=(x[i]-s)*(x[i]-s);
returnsqrt(sum/10);
}
main()
{doubles,x[10]={95.0,89.0,76.0,65.0,88.0,72.0,85.0,81.0,90.0,56.0};
inti;
printf("\nTheoriginaldatais:
\n");
for(i=0;i<10;i++)printf("%6.1f",x[i]);printf("\n\n");
s=fun(x);
printf("s=%f\n\n",s);
}
B类最大值和最小值(1—4)
1
#include
#include
voidfun(inta[],intn,int*max,int*d)
{
inti;
*d=0;*max=a[0];
for(i=0;i if(a[i]>a[*d]) { *d=i; *max=a[*d]; } } main() {inti,x[20],max,index,n=10; randomize(); for(i=0;i printf("\n"); fun(x,n,&max,&index); printf("Max=%5d,Index=%4d\n",max,index); } 2 #include #defineN80 voidfun(int*s,int*t,int*k) { inti; *k=0; for(i=1;i<*t;i++) if(s[i]>s[*k]) *k=i; s[*t]=s[*k]; *t=*t+1; } main() {inta[N]={876,675,896,101,301,401,980,431,451,777},i,n,mi; n=10; printf("\nTheoriginaldata: \n"); for(i=0;i fun(a,&n,&mi); printf("Theindexofmaxis: %d\n",mi); printf("Theresult: \n"); for(i=0;i } 3 #include #include #defineM3 #defineN4 voidfun(inttt[M][N],intpp[N]) { inti,j; for(j=0;j { pp[j]=tt[0][j]; for(i=1;i if(tt[i][j] pp[j]=tt[i][j]; } } main() {intt[M][N]={{22,45,56,30}, {19,33,45,38}, {20,22,66,40}}; intp[N],i,j,k; printf("Theoriginaldatais: \n"); for(i=0;i for(j=0;j printf("%6d",t[i][j]); printf("\n"); } fun(t,p); printf("\nTheresultis: \n"); for(k=0;k printf("\n"); } 4 #include #include intfun(int*s,intt,int*k) { inti; *k=0; for(i=1;i if(s[i]>s[*k]) *k=i; } main() { inta[10]={876,675,896,101,301,401,980,431,451,777},k; clrscr(); fun(a,10,&k); printf("%d,%d\n",k,a[k]); } C类二维数组行列下标(1—11) 1 #include #include #include #defineN5 intfun(inta[][N],intm) { inti,j; for(i=0;i for(j=i;j a[i][j]*=m; } main() {inta[N][N],m,i,j; printf("*****Thearray*****\n"); for(i=0;i {for(j=0;j {a[i][j]=rand()%20;printf("%4d",a[i][j]);} printf("\n"); } dom=rand()%10;while(m>=3); printf("m=%4d\n",m); fun(a,m); printf("THERESULT\n"); for(i=0;i {for(j=0;j printf("\n"); } } } } 2 #include #include intfun(intarray[3][3]) { intt,i,j; for(i=0;i<2;i++) for(j=1;j<3;j++) { t=array[i][j]; array[i][j]=array[j][i]; array[j][i]=t; } } main() { inti,j; intarray[3][3]={{100,200,300}, {400,500,600}, {700,800,900}}; for(i=0;i<3;i++) {for(j=0;j<3;j++) printf("%7d",array[i][j]); printf("\n"); } fun(array); printf("Convertedarray: \n"); for(i=0;i<3;i++) {for(j=0;j<3;j++) printf("%7d",array[i][j]); printf("\n"); } } 3 #include #include #include #defineN5 doublefun(intw[][N]) { inti,j; doublesum=0; for(j=0;j sum+=w[0][j]+w[N-1][j]; for(i=1;i sum+=w[i][0]+w[i][N-1]; returnsum; } main() {inta[N][N]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1}; inti,j; doubles; printf("*****Thearray*****\n"); for(i=0;i {for(j=0;j {printf("%4d",a[i][j]);} printf("\n"); } s=fun(a); printf("*****THERESULT*****\n"); printf("Thesumis: %lf\n",s); } 5 #include voidfun(int(*s)[10],int*b,int*n,intmm,intnn) { inti,j; *n=0; for(i=0;i for(j=0;j b[(*n)++]=s[i][j]; } main() {intw[10][10]={{33,33,33,33},{44,44,44,44},{55,55,55,55}},i,j; inta[100]={0},n=0; printf("Thematrix: \n"); for(i=0;i<3;i++) {for(j=0;j<4;j++)printf("%3d",w[i][j]); printf("\n"); } fun(w,a,&n,3,4); printf("TheAarray: \n"); for(i=0;i } 6 #include voidfun(ints[][10],intb[],int*n,intmm,intnn) { /*以下代码仅供参考*/ inti,j,np=0;/*np用作b数组下标*/ for(i=0;i for(j=0;j b[np++]=s[j][i]; *n=np; } main() {intw[10][10]={{33,33,33,33},{44,44,44,44},{55,55,55,55}},i,j; inta[100]={0},n=0; printf("Thematrix: \n"); for(i=0;i<3;i++) {for(j=0;j<4;j++)printf("%3d",w[i][j]); printf("\n"); } fun(w,a,&n,3,4); printf("TheAarray: \n"); for(i=0;i } _ D类素数(1—5) 1 #include #include voidfun(intm,int*k,intxx[]) { inti,j; *k=0; for(i=2;i { for(j=2;j if(i%j==0)break; if(j } } main() { intm,n,zz[100]; printf("\nPleaseenteranintegernumberbetween10and100: "); scanf("%d",&n); fun(n,&m,zz); printf("\n\nThereare%dnon-primenumberslessthan%d: ",m,n); for(n=0;n printf("\n%4d\n",zz[n]); } 2 将大于形参m且紧靠m的k个素数存入xx数组中。 voidfun(intm,intk,intxx[]) { /*以下代码仅供参考*/ inti,j=1,t=m+1; while(j<=k) { /*以下完成判断素数,并存放到数组xx中*/ for(i=2;i if(t%i==0)break; if(i>=t){xx[j-1]=t;j++;} t++; } } main() { intm,xx[100],k,i; scanf("%d,%d",&m,&k); fun(m,k,xx); for(i=0;i printf("%3d",xx[i]); } 3 #include #include doublefun(intn) { inti,j; doubles=0; for(i=3;i { for(j=2;j if(i%j==0)break; if(j>=i)s+=sqrt(i); } returns; } main() {intn;doublesum; printf("\n\nInputn: ");scanf("%d",&n); sum=fun(n); printf("\n\nsum=%f\n\n",sum); } E数学其它(1—15) 1 #include #include voidfun(inta,intb,long*c) { *c=a/10*1000+a%10*10+b/10+b%10*100; } main() {inta,b;longc; printf("Inputa,b: "); scanf("%d%d",&a,&b); fun(a,b,&c); printf("Theresultis: %ld\n",c); } 2 #include #include #defineM200 intaa[1000]; voidfun(int*n) { inti; *n=0; for(i=1;i<1000;i++) if((i%7==0||i%11==0)&&i%77! =0) aa[(*n)++]=i; } main() {intn,k; fun(&n); for(k=0;k if((k+1)%10==0)printf("\n"); elseprintf("%5d",aa[k]); } 3 #include #include unsignedfun(unsignedw) { longt=10; while(w/t! =0) t*=10; returnw%(t/10); } main() {unsignedx; printf("Enteraunsignedintegernumber: ");scanf("%u",&x); printf("Theoriginaldatais: %u\n",x); if(x<10)printf("Dataerror! "); elseprintf("Theresult: %u\n",fun(x)); } 4 #include #include doublefun(doublex[9]) { inti; doubles=0; for(i=1;i<9;i++) s=s+sqrt((x[i-1]+x[i])/2); returns; } main() {doubles,a[9]={12.0,34.0,4.0,23.0,34.0,45.0,18.0,3.0,11.0}; inti; printf("\nTheoriginaldatais: \n"); for(i=0;i<9;i++)printf("%6.1f",a[i]);printf("\n\n"); s=fun(a); printf("s=%f\n\n",s); } 5 #include #include #defineN20 fun(int*a,intn,int*odd,int*even) { inti; *odd=0; *even=0; for(i=0;i if(a[i]%2! =0) *odd=*odd+a[i]; else *even+=a[i]; } main() {inta[N]={1,9,2,3,11,6},i,n=6,odd,even; printf("Theoriginaldatais: \n"); for(i=0;i printf("\n\n"); fun(a,n,&odd,&even); printf("Thesumofoddnumbers: %d\n",odd); printf("Thesumofevennumbers: %d\n",even); } 6 #include #include floatfun(floath) { h=h+0.005; return(long)(h*100)/100.0; } main() {floata; printf("Entera: ");sca
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 上机 编程 答案