Res - 2009夏学期C语言上机练习参考答案汇总 下载本文

夏学期C程序设计上机练习参考答案

夏1周 ..........................................................................................................................................................................................................1 夏5周 ..........................................................................................................................................

70011 简化的插入排序 ..............................................................................................................................................................1 10014 计算函数P(n,x) .................................................................................

70012 求平均值 .........................................................................................................................................................................3 10016 十进制转换二进制 ............................................................................70013 将数组中的数逆序存放 ..................................................................................................................................................3 10017 递归函数程序设计求Fabonacci数70014 求最大值及其下标 ..........................................................................................................................................................4 列 25

70015 交换最小值和最大值 ......................................................................................................................................................4 10019 改错题error10_1.cpp .......................................................................

70016 选择法排序 .....................................................................................................................................................................5 10022 编程题 ................................................................................................

70017 在数组中查找指定的元素 ..............................................................................................................................................5 10026 指定位置输出字符串 ........................................................................

70021 求矩阵各行元素之和 ......................................................................................................................................................6 10027 藏尾诗 ................................................................................................

70022 矩阵运算 .........................................................................................................................................................................6 10028 改错题error11_2.cpp........................................................................

70023 九九乘法表 .....................................................................................................................................................................7 40065 分解质因数 ........................................................................................

夏2周 ..........................................................................................................................................................................................................7 40067 打印图案 ............................................................................................

70024 判断上三角矩阵....................................................................................................................................................................8 夏6周 ..........................................................................................................................................

70025 算算看,这是第几天? ........................................................................................................................................................8 30062 输出21世纪所有闰年 ....................................................................................

70026 找鞍点(选作) .........................................................................................................................................................................9 90001 调试示例error09_1.cpp .................................................................................

70031 将字符串逆序存放 ................................................................................................................................................................9 90002 时间换算 ..........................................................................................................70032 查找字符 .............................................................................................................................................................................10 90003 计算平均成绩 .................................................................................................

70033 统计大写辅音字母 ..............................................................................................................................................................10 90004 计算两个复数之积 ..........................................................................................

70034 字符串替换 ......................................................................................................................................................................... 90005 查找书籍 ..........................................................................................................11

70035 将十六进制字符串转换为十进制整数 ..............................................................................................................................12 90006 通讯录排序 ......................................................................................................

70036 将十进制字符串转换为十进制整数 ..................................................................................................................................12 90007 算算看,这是第几天? ..................................................................................

70052 统计字符出现次数 ..............................................................................................................................................................13 90008 使用函数实现时间换算 ..................................................................................

夏3周 ........................................................................................................................................................................................................13 90009 找出总分最高的学生 ......................................................................................

10008 求1~100中能被6整除的所有整数的和 ....................................................................................................................13 其它练习 ......................................................................................................................................

20014 计算三门课程的平均成绩 ............................................................................................................................................13 20027计算旅途时间。 .....................................................................................................

20016 计算x的平方 ................................................................................................................................................................14 20028数字加密 .................................................................................................................

20021 计算分段函数的值 ........................................................................................................................................................14 教材习题3-4 (上机练习30009,统计学生成绩) ............................................................

20022 计算摄氏温度 ................................................................................................................................................................14 30061 出租车计费 ............................................................................................................

70051 找最大值并交换 ............................................................................................................................................................14 教材习题4-12(p77) ........................................................................................................

80011 循环移动 ........................................................................................................................................................................15 教材习题4-14(p77) ........................................................................................................80012 在数组中查找指定元素 ................................................................................................................................................16 50051 数字金字塔(此题与40067打印图案的思路相似)

80013 使用函数的选择法排序 ................................................................................................................................................16 ...........................................................................................................................................80014 报数 ...............................................................................................................................................................................17 夏4周 ........................................................................................................................................................................................................19 夏1周

10012 函数程序设计 ................................................................................................................................................................19 70011 简化的插入排序 10024 计算最长的字符串长度 ................................................................................................................................................19 输入一个正整数 repeat (0

1

夏学期C程序设计上机练习参考答案

3 (待插入整数x=3) 4 (数据的个数n=4) 1 2 5 7 (4个有序整数) -10 (待插入整数x=-10) 3 (数据的个数n=3) 1 2 4 (3个有序整数) 100 (待插入整数x=100) 5 (数据的个数n=5) 1 2 4 5 7 (5个有序整数) 4 (待插入整数x=4) 输出: 1 2 3 4 5 7 -10 1 2 5 7 1 2 4 100 1 2 4 4 5 7 参考解1: #include int main(void) { int i, n, x; int repeat, ri; int a[10]; scanf(\ for(ri = 1; ri <= repeat; ri++){ scanf(\ for(i = 0; i < n; i++) scanf(\ scanf(\ for (i = n-1; i >= 0; i--) { if (x < a[i]) { a[i+1]=a[i]; a[i] = x; /* 保证最前面的元素 for(i = 0; i < n + 1; i++) printf(\ putchar('\\n'); } } 参考解2: #include int main(void) { int i, j, n, x; int repeat, ri; int a[10]; scanf(\ for(ri = 1; ri <= repeat; ri++){ scanf(\ for(i = 0; i < n; i++) scanf(\ scanf(\ a[n]=x; /* 将被比较的数放到最后,然后往前对比插入 */ for(i=0;ia[n]){ j=a[i]; a[i]=a[n]; a[n]=j; } } for(i = 0; i < n + 1; i++) printf(\ putchar('\\n'); } } 参考解3: 插入正确 */ } else { a[i+1] = x; /* 保证最后面的元素#include int main(void) { int i, j, n, x; int repeat, ri; 插入正确 */ } break; } int a[10]; scanf(\ 2

夏学期C程序设计上机练习参考答案

for(ri = 1; ri <= repeat; ri++){ scanf(\ for(i = 0; i < n; i++) scanf(\ scanf(\ if (x >= a[n-1]) a[n] = x; /* 特殊情况:若x比

#include int main(void) { int i, n, sum; int repeat, ri; int a[10]; double average; scanf(\ for(ri = 1; ri <= repeat; ri++){ 所有的元素都要大 */ else for(i = 0; i < n; i++) { if(x > a[i]) continue; /* 将x 插入到合适的位置*/ j = n - 1; /* 从当前要插入的位置 scanf(\ for(i = 0; i < n; i++) 往后移位 */ */ break; } while(j >= i){ a[j+1] = a[j]; j--; } scanf(\ sum=0; for(i=0; i < n; i++) sum+=a[i]; a[i] = x; /* 将x查到当前的位置 average=1.0*sum/n*1.0; printf(\ } }

70013 将数组中的数逆序存放

输入一个正整数 repeat (0

输入一个正整数 n (1

输入输出示例:括号内为说明 输入

2 (repeat=2) 4 (n=4) 10 8 1 2

5 (n=5) 1 2 5 4 0 输出 2 1 8 10 0 4 5 2 1

#include int main(void) { int i, n, temp; for(i = 0; i < n + 1; i++) printf(\ putchar('\\n'); } }

70012 求平均值

输入一个正整数 repeat (0

输入一个正整数 n (1

输入输出示例:括号内为说明 输入

2 (repeat=2) 3 (n=3) 1 2 -6 5 (n=5) 12 2 5 4 0 输出

average = -1.00 average = 4.60

3

夏学期C程序设计上机练习参考答案

int repeat, ri; int a[10]; scanf(\ for(ri = 1; ri <= repeat; ri++){ scanf(\ for(i = 0; i < n; i++) scanf(\ i=0; for(i=0;i

70014 求最大值及其下标

输入一个正整数 repeat (0

输入一个正整数 n (1

3 (repeat=3) 3 (n=3) 1 6 4 4 (n=4) 10 8 1 9 5 (n=5) 1 2 0 4 5 输出

max = 6, index = 1 (最大值6的下标是1) max = 10, index = 0 (最大值10的下标是0) max = 5, index = 4 (最大值5的下标是4) #include int main(void) { int i, index, n; printf(\ } }

70015 交换最小值和最大值

输入一个正整数 repeat (0

输入一个正整数 n (1

输入输出示例:括号内为说明 输入

3 (repeat=3) 5 (n=5) 8 2 5 1 4 4 (n=4) 1 5 6 7

5 (n=5) 5 4 3 2 1 输出

After swap: 1 2 5 4 8 After swap: 1 5 6 7 After swap: 1 4 3 2 5 #include int main(void) { 4