浙江大学城市学院《程序设计2》上机代码
夏1周2
70011 简化的插入排序2
70012 求平均值5
70013 将数组中的数逆序存放6
70014 求最大值及其下标7
70015 交换最小值和最大值8
70016 选择法排序9
70017 在数组中查找指定的元素10
70021 求矩阵各行元素之和11
70022 矩阵运算12
70023 九九乘法表13
夏2周14
70024 判断上三角矩阵14
70025 算算看,这是第几天?15
70026 找鞍点(选作) 16
70031 将字符串逆序存放17
70032 查找字符18
70033 统计大写辅音字母19
70034 字符串替换20
70035 将十六进制字符串转换为十进制整数21
70036 将十进制字符串转换为十进制整数22
70052 统计字符出现次数23
夏3周24
10008 求1~100中能被6整除的所有整数的和24
20014 计算三门课程的平均成绩25
20016 计算x的平方25
20021 计算分段函数的值25
20022 计算摄氏温度26
70051 找最大值并交换27
80011 循环移动28
80012 在数组中查找指定元素29
80013 使用函数的选择法排序30
80014 报数32
夏4周35
10012 函数程序设计35
10024 计算最长的字符串长度36
10025 字符串的连接37
40017 求2/1+3/2+5/3+8/5+ (38)
80021 找最大的字符串39
80022 找最长字符串40
80023 使用函数删除字符串中的字符41
80024 使用函数实现字符串复制42
80025 判断回文字符串43
80026 分类统计字符个数44
夏5周45
10014 计算函数P(n,x) 45
10016 十进制转换二进制46
10017 递归函数程序设计求Fabonacci数列48
10019 改错题error10_1.cpp 49
10022 编程题50
10026 指定位置输出字符串50
10027 藏尾诗51
10028 改错题error11_2.cpp 52
40065 分解质因数53
40067 打印图案54
夏6周56
30062 输出21世纪所有闰年56
90001 调试示例error09_1.cpp 56
90002 时间换算57
90003 计算平均成绩58
90004 计算两个复数之积59
90005 查找书籍60
90006 通讯录排序61
90007 算算看,这是第几天?62
90008 使用函数实现时间换算63
90009 找出总分最高的学生64
其它练习65
20027计算旅途时间。65
20028数字加密66
教材习题3-4 (上机练习30009,统计学生成绩) 66
30061 出租车计费67
教材习题4-12(p77) 68
教材习题4-14(p77) 69
50051 数字金字塔(此题与40067打印图案的思路相似) 69
夏1周
70011 简化的插入排序
输入一个正整数repeat (0 输入一个正整数n(0 输入输出示例:括号内为说明 输入: 4 (repeat=4) 5 (数据的个数n=5) 1 2 4 5 7 (5个有序整数) 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("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &x); for (i = n-1; i >= 0; i--) { if (x < a[i]) { a[i+1]=a[i]; a[i] = x; } else { a[i+1] = x; break; } } for(i = 0; i < n + 1; i++) printf("%d ", a[i]); putchar('\n'); } } 参考解2: #include int main(void) { int i, j, n, x; int repeat, ri; int a[10]; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &x); a[n]=x; for(i=0;i if(a[i]>a[n]){ j=a[i]; a[i]=a[n]; a[n]=j; } } for(i = 0; i < n + 1; i++) printf("%d ", a[i]); putchar('\n'); } } 参考解3: #include int main(void) { int i, j, n, x; int repeat, ri; int a[10]; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &x); if (x >= a[n-1]) a[n] = x; else for(i = 0; i < n; i++) { if(x > a[i]) continue; j = n - 1; while(j >= i){ a[j+1] = a[j]; j--; } a[i] = x; break; } for(i = 0; i < n + 1; i++) printf("%d ", a[i]); 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 #include int main(void) { int i, n, sum; int repeat, ri; int a[10]; double average; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); sum=0; for(i=0; i < n; i++) sum+=a[i]; average=1.0*sum/n*1.0; printf("average = %.2f\n", average); } } 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; int repeat, ri; int a[10]; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); i=0; for(i=0;i temp=a[i]; a[i]=a[n-1-i]; a[n-1-i]=temp; } for(i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); } } 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; int ri, repeat; int a[10]; scanf("%d", &repeat); for(ri = 1; ri <= repeat; ri++){ scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); index=0; a[index]=a[0]; for(i = 1; i < n; i++){ if(a[index] a[index]=a[i]; index=i; } } printf("max = %d, index = %d\n", a[index], index); } } 70015 交换最小值和最大值