当前位置:文档之家› 第二次上机作业参考答案

第二次上机作业参考答案

第二次上机作业参考答案:1. 4个数排序从键盘任意输入4个整数,将其按从小到大顺序排序后输出。

例:输入 5 12 -9 3输出After ascending sorting: -9 3 5 12#include <stdio.h>void main(){ int a,b,c,d,t;scanf("%d%d%d%d",&a,&b,&c,&d);if(a>b){ t=a; a=b; b=t; }if(a>c){ t=a; a=c; c=t; }if(a>d){ t=a; a=d; d=t; }if(b>c){ t=b; b=c; c=t; }if(b>d){ t=b; b=d; d=t; }if(c>d){ t=c; c=d; d=t; }printf("The sorted number is:%d %d %d %d\n",a,b,c,d);}2.求1 + 1/2! +....+ 1/n!输入正整数n,计算上式前n 项的和(保留 4 位小数)。

例:输入10输出 1 + 1/2! +....+ 1/10!=1.7183#include <stdio.h>int main( ){ int i,j,n;float s,t;scanf("%d",&n);for(s=0,i=1;i<=n;i++){for(t=1,j=1;j<=i;j++)t*=j;s+=1/t;}printf("%0.4f\n",s);}3. 编程计算表达式:data1 op data2 的值。

(+ - * / %)例:输入23+12输出23+12=35#include <stdio.h>int main( ){ int a,b,result,flag=1;char op;scanf("%d%c%d",&a,&op,&b);switch(op){ case '+': result=a+b;break;case '-': result=a-b;break;case '*': result=a*b;break;case '/': printf("%d%c%d=%.2f\n",a,op,b,(float)a/b);flag=0;break;case '%': result=a%b;break;}if(flag)printf("%d%c%d=%d\n",a,op,b,result);}4. 用二分法求方程2x3-4x2+3x-6=0在(a,b)之间的根。

例:输入Please input the lower and upper boundaries: 3 10 输出No root in this boundary!Please input the lower and upper boundaries: 1 5The root is 2.00#include <math.h>void main(){ double x,fx,a,fa,b,fb;do{ printf("Please input the lower and upper boundaries:");scanf("%lf%lf",&a,&b);fa=2*a*a*a-4*a*a+3*a-6;fb=2*b*b*b-4*b*b+3*b-6;}while(fa*fb>0);while(1){ x=(a+b)/2;fx=2*x*x*x-4*x*x+3*x-6;if(fabs(fx)<=1e-5){ printf("The solution is %.2f\n",x);break;}if(fx*fa<0){ b=x; fb=fx; }else{ a=x; fa=fx; }}}5.日历显示编程反复显示2013年各月份日历。

(程序可以设定2013年1月1号为星期二)。

程序运行输出示例如下:Enter month in 2013 to display calendar (1-12): 5****************************************************SUN MON TUE WED THU FRI SAT1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31Would you like to display another month (y/n) ? yEnter month in 2013 to display calendar (1-12): 13ERROR--- Enter month in range of (1-12)!Enter month in 2013 to display calendar (1-12):10****************************************************SUN MON TUE WED THU FRI SAT1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31Would you like to display another month (y/n) ? N#include <stdio.h>void main(){int year=2013,month,days=0;int dayinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};int fday=2; /*the first day of 2013 is Tuesday*/int i;char ch;while(1){while(1){printf("Enter month in 2013 to display calendar (1-12):");scanf("%d",&month);if(month<1 || month>12)printf("ERROR- Enter month in range of (1-12)!\n");elsebreak;}days=0;fday=2;/*how many days are there from Jan to month-1*/for(i=0;i<month-1;i++)days+=dayinmonth[i];fday=(days+fday)%7; /*what day is first day of month*/printf("*************************************************\n");printf(" SUN MON TUE WED THU FRI SAT\n");for(i=0;i<fday;i++)printf(" ");for(i=1;i<=dayinmonth[month-1];i++)if((i+fday)%7==0)printf("%7d\n",i);elseprintf("%7d",i);printf("\n");printf("Would you like to display another month (y/n)?");fflush(stdin);ch=getchar();if(ch=='n' || ch=='N')break;}}思考题:某地刑侦大队对涉及六个嫌疑人的一桩疑案进行分析:➢A、B至少有一人作案;➢A、E、F三人中至少有两人参与作案;➢A、D不可能是同案犯;➢B、C或同时作案,或与本案无关;➢C、D中有且仅有一人作案;➢如果D没有参与作案,则E也不可能参与作案。

试编一程序,将作案人找出来。

#include <stdio.h>void main() // 主函数{int a,b,c,d,e,f,cc1,cc2,cc3,cc4,cc5,cc6;char info[][20]={" is not crimal."," is a crimal"};for(a=0;a<=1;a++)for(b=0;b<=1;b++)for(c=0;c<=1;c++)for(d=0;d<=1;d++)for(e=0;e<=1;e++)for(f=0;f<=1;f++){ cc1=a||b;cc2=a&&e || a&&f || e&&f;cc3=!(a&&d);cc4=b&&c || !b&&!c;cc5=c&&!d || d&&!c;cc6=d||!e;if(cc1+cc2+cc3+cc4+cc5+cc6==6){ printf("A:%s\n",info[a]);printf("B:%s\n",info[b]);printf("C:%s\n",info[c]);printf("D:%s\n",info[d]);printf("E:%s\n",info[e]);printf("F:%s\n",info[f]);printf("\n");}}}五位跳水高手将参加十米高台跳水决赛,有好事者让五个人据实力预测比赛结果。

➢A选手说:B第二,我第三;➢B选手说:我第二,E第四;➢C选手说:我第一,D第二;➢D选手说:C最后,我第三;➢E选手说:我第四,A第一;决赛成绩公布之后,每位选手的预测都只说对了一半,即一对一错,请编程解出比赛的实际名次。

#include <stdio.h>void main(){ int a,b,c,d,e,ta,tb,tc,td,te,t;for(a=1;a<=5;a++)for(b=1;b<=5;b++)for(c=1;c<=5;c++)for(d=1;d<=5;d++){ e=15-a-b-c-d;≠≠≠≠*/if(a*b*c*d*e==120) /*a b c d e{ ta=((b==2)+(a==3))==1;tb=((b==2)+(e==4))==1;tc=((c==1)+(d==2))==1;td=((c==5)+(d==3))==1;te=((e==4)+(a==1))==1;t=ta+tb+tc+td+te;if(t==5)printf("a=%d, b=%d, c=%d, d=%d, e=%d\n",a,b,c,d,e);}}}。

相关主题