页数:6页文件大小:34.2 KB来源: C语言程序设计ppt课件
本课件布置第二次上机作业,包含四个数排序、求阶乘倒数和、计算data1 op data2表达式、二分法求方程根与日历显示五道题目,给出输入输出示例,随后提供参考答案代码,覆盖起泡式比较交换、阶乘累加、switch分支求值与二分法迭代,并附两道枚举类思考题。
第二次上机作业要求:独立完成,程序必须采用缩进格式,上机调试通过后上交;以附件形式将所有程序源程序压缩打包提交。
#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); } 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); 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); } #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; ... } while (...); }运行示例:输入 1 显示 1 月日历(SUN MON TUE WED THU FRI SAT 表头,1 号排在星期三位置);输入 13 提示 ERROR --- Enter month in range of (1-12)!;输入 4 显示 4 月日历;询问 Would you like to display another month(y/n)? 输入 N 结束。
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); else printf("%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 也未参与。
int a,b,c,d,e,f,cc1,cc2,cc3,cc4,cc5,cc6; char info[][20] = {"is not crimal.", "is a crimal"}; 五位跳水高手参加十米高台跳水决赛,每人预测都只说对一半:A 说 B 第二、我第三;B 说我第二、E 第四;C 说我第一、D 第二;D 说 C 最后、我第三;E 说我第四、A 第一。
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) { 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); } }下载地址:{{downurl}}
{{{a}}}
{{#b}}{{{b}}}
{{/b}} {{/title}} {{#nolog}}登陆注册
{{/nolog}} {{#djsx}}点我刷新
{{/djsx}} {{#gocz}} {{/gocz}} {{#ffqr}} {{/ffqr}}