目录
问题 A: 习题6-4 有序插入
题目描述
有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入到数组中。
假设数组长度为10,数组中前9个数(这9个数要求从键盘上输入,输入时要满足自小到大的输入顺序)已经按从小到大进行排序。
然后再从键盘上输入一个整数,将此整数插入到前有序的9个数中,使得最终的10个数依然是从小到大有序的。
输入
第一行输入以空格分隔的9个整数数,要求按从小到大的顺序输入。
第二行输入一个整数
输出
从小到大输出这10个数,每个数一行。
样例输入
1 11 21 31 41 51 61 71 81
45
样例输出
1
11
21
31
41
45
51
61
71
81
提示
定义数组时,把数组长度定义为10.
题解
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
int str[10];
int i;
for(i = 0; i < 9; i++){
scanf("%d", &str[i]);
}
int temp, test;
scanf("%d", &temp);
for(i = 0; i < 9; i++){
if(temp >= str[i]){
printf("%d\n", str[i]);
}
else{
printf("%d\n", temp);
test = i;//暂存比temp大的数的位置
break;
}
}
for(i = test; i < 9; i++){
printf("%d\n", str[i]);
}
return 0;
}
问题 B: 习题6-5 数组元素逆置
题目描述
将一个长度为10的整型数组中的值按逆序重新存放。
如:原来的顺序为1,2,3,4,5,6,7,8,9,0,要求改为0,9,8,7,6,5,4,3,2,1
输入
从键盘上输入以空格分隔的10个整数。
输出
按相反的顺序输出这10个数,每个数占一行。
样例输入
1 2 3 4 5 6 7 8 9 0
样例输出
0
9
8
7
6
5
4
3
2
1
题解
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
int str[10];
int i;
for(i = 0; i < 10; i++){
scanf("%d", &str[i]);
}
for(i = 9; i >= 0; i--){
printf("%d\n", str[i]);
}
return 0;
}
问题 C: 习题6-6 杨辉三角
题目描述
按要求输入如下格式的杨辉三角
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
最多输出10层
输入
输入只包含一个正整数n,表示将要输出的杨辉三角的层数。
输出
对应于该输入,请输出相应层数的杨辉三角,每一层的整