topic:
describe
Input two sequences in ascending order , merge the two sequences into one sorted sequence and output .
Enter a description:
The input consists of three lines ,
the first line contains two positive integers n , m separated by spaces . n indicates the number of numbers in the first ascending sequence in the second line , and m indicates the number of numbers in the second ascending sequence in the third line .
The second line contains n integers separated by spaces .
The third line contains m integers separated by spaces.
Output description:
The output is one line , and the output is an ascending sequence of length n+m , that is, the elements in the ascending sequence of length n and the ascending sequence of length m are re-arranged and merged in ascending sequence .
=========================================================================
Ideas:
general idea:
(one).
Enter n and m ,
Define variable-length arrays arr1 and arr2 (Niuke.com supports arrays),
Assign values to variable-length arrays
(two).
Merge : define the third variable-length array arr3 , respectively define the subscripts i , j , k of the three arrays ,
Use a while loop , merge when arr1 and arr2 have values , merge into arr3 ,
Use the if conditional judgment statement to assign the value of arr1 smaller than arr2 to arr3 , adjust the subscripts of the two arrays ,
Use the else statement to assign the value equal to or arr2 < arr1 to arr3 , and adjust the subscripts of the two arrays
(three).
After completing the steps in (2),
Jumping out of the loop means that an array has been traversed .
At this time, the i, j, and k subscripts have all moved to the appropriate positions .
First determine which array has been traversed :
If i == n , it means that arr1 has finished traversing , then put all the remaining elements in arr2 into arr3 ;
In other cases (j == m) , it means that arr2 has finished traversing , then put all the remaining elements in arr1 into arr3 .
to print
first step:
(1). Input n and m
(2). Define variable-length arrays arr1 and arr2
(3). Assign values to variable-length arrays
Implementation code:
#include <stdio.h> int main() { //输入 n 和 m int n = 0; int m = 0; scanf("%d %d", &n, &m); //定义变长数组: int arr1[n]; int arr2[m]; //对变长数组进行赋值 int i = 0; for (i = 0; i < n; i++) //前面输入的n就是arr1的长度 { scanf("%d", &arr1[i]); } i = 0; for (i = 0; i < m; i++) //前面输入的m就是arr1的长度 { scanf("%d", &arr2[i]); } return 0; }
Realize the picture:
Step two:
Start the merge:
(1). Define the third variable-length array arr3 , and define the subscripts of three variable-length arrays : i , j , k
(2). Use a while loop to merge when both arr1 and arr2 have values , and merge them into arr3 ,
(3). Use the if conditional judgment statement to assign the value of arr1 smaller than arr2 to arr3 , and adjust the subscripts of the two arrays .
Use the else statement to assign the value equal to or arr2 < arr1 to arr3 , and adjust the subscripts of the two arrays
Implementation code:
#include <stdio.h> int main() { //输入 n 和 m int n = 0; int m = 0; scanf("%d %d", &n, &m); //定义变长数组: int arr1[n]; int arr2[m]; //对变长数组进行赋值 int i = 0; for (i = 0; i < n; i++) //前面输入的n就是arr1的长度 { scanf("%d", &arr1[i]); } i = 0; for (i = 0; i < m; i++) //前面输入的m就是arr1的长度 { scanf("%d", &arr2[i]); } //开始合并: //定义第三个变长数组arr3,存放两arr1 和 arr2合并后的结果: int arr3[n + m]; //变长数组不能初始化 //定义三个变长数组的下标: i = 0; //arr1下标 int j = 0; //arr2下标 int k = 0; //arr3下标 //使用 while循环, while (i<n && j<m) //i<n:arr1下标小于arr1长度,说明arr1还有值 //j<m:arr2下标小于arr2长度,说明arr2还有值 //两个数组都有值才用进行比较赋值 { if (arr1[i] < arr2[j]) //arr1的值小于arr2的值 { arr3[k] = arr1[i]; //把大的值赋给arr3 //给值 和 被给值 的数组下标都用往后移 k++; i++; } else //等于时放谁都一样 或 arr2变得比较小了 { arr3[k] = arr2[j]; //把大的值赋给arr3 //给值 和 被给值 的数组下标都用往后移 k++; j++; } } return 0; }
Realize the picture:
third step:
After completing the steps in (2),
Jumping out of the loop means that an array has been traversed .
At this time, the i, j, and k subscripts have all moved to the appropriate positions .
(1). First determine which array has been traversed :
If i == n , it means that arr1 has finished traversing , then put all the remaining elements in arr2 into arr3 ;
In other cases (j == m) , it means that arr2 has finished traversing , then put all the remaining elements in arr1 into arr3 .
(2). Print
Implementation code:
#include <stdio.h> int main() { //输入 n 和 m int n = 0; int m = 0; scanf("%d %d", &n, &m); //定义变长数组: int arr1[n]; int arr2[m]; //对变长数组进行赋值 int i = 0; for (i = 0; i < n; i++) //前面输入的n就是arr1的长度 { scanf("%d", &arr1[i]); } i = 0; for (i = 0; i < m; i++) //前面输入的m就是arr1的长度 { scanf("%d", &arr2[i]); } //开始合并: //定义第三个变长数组arr3,存放两arr1 和 arr2合并后的结果: int arr3[n + m]; //变长数组不能初始化 //定义三个变长数组的下标: i = 0; //arr1下标 int j = 0; //arr2下标 int k = 0; //arr3下标 //使用 while循环, while (i < n && j < m) //i<n:arr1下标小于arr1长度,说明arr1还有值 //j<m:arr2下标小于arr2长度,说明arr2还有值 //两个数组都有值才用进行比较赋值 { if (arr1[i] < arr2[j]) //arr1的值小于arr2的值 { arr3[k] = arr1[i]; //把大的值赋给arr3 //给值 和 被给值 的数组下标都用往后移 k++; i++; } else //等于时放谁都一样 或 arr2变得比较小了 { arr3[k] = arr2[j]; //把大的值赋给arr3 //给值 和 被给值 的数组下标都用往后移 k++; j++; } } //跳出循环说明有一个数组已经被遍历完了 //此时,i、j、k 都已经移动到了合适的位置 //判断哪个数组遍历完了进行操作: if (i == n) //arr1下标等于arr1数组长度,说明arr1遍历完了 { //将arr2中剩余的元素全部放入arr3中: while (j < m)//arr2下标小于arr2数组长度就继续放 { arr3[k] = arr2[j]; k++; j++; } } else //arr2遍历完 { //将arr1中剩余的元素全部放入arr3中: while (i < n)//arr1下标小于arr1数组长度就继续放 { arr3[k] = arr1[i]; k++; i++; } } //进行打印: for (i = 0; i < n + m; i++) { printf("%d ", arr3[i]); } return 0; }
Realize the picture:
Final code and implementation effect
Final code:
#include <stdio.h> int main() { //输入 n 和 m int n = 0; int m = 0; scanf("%d %d", &n, &m); //定义变长数组: int arr1[n]; int arr2[m]; //对变长数组进行赋值 int i = 0; for (i = 0; i < n; i++) //前面输入的n就是arr1的长度 { scanf("%d", &arr1[i]); } i = 0; for (i = 0; i < m; i++) //前面输入的m就是arr1的长度 { scanf("%d", &arr2[i]); } //开始合并: //定义第三个变长数组arr3,存放两arr1 和 arr2合并后的结果: int arr3[n + m]; //变长数组不能初始化 //定义三个变长数组的下标: i = 0; //arr1下标 int j = 0; //arr2下标 int k = 0; //arr3下标 //使用 while循环, while (i < n && j < m) //i<n:arr1下标小于arr1长度,说明arr1还有值 //j<m:arr2下标小于arr2长度,说明arr2还有值 //两个数组都有值才用进行比较赋值 { if (arr1[i] < arr2[j]) //arr1的值小于arr2的值 { arr3[k] = arr1[i]; //把大的值赋给arr3 //给值 和 被给值 的数组下标都用往后移 k++; i++; } else //等于时放谁都一样 或 arr2变得比较小了 { arr3[k] = arr2[j]; //把大的值赋给arr3 //给值 和 被给值 的数组下标都用往后移 k++; j++; } } //跳出循环说明有一个数组已经被遍历完了 //此时,i、j、k 都已经移动到了合适的位置 //判断哪个数组遍历完了进行操作: if (i == n) //arr1下标等于arr1数组长度,说明arr1遍历完了 { //将arr2中剩余的元素全部放入arr3中: while (j < m)//arr2下标小于arr2数组长度就继续放 { arr3[k] = arr2[j]; k++; j++; } } else //arr2遍历完 { //将arr1中剩余的元素全部放入arr3中: while (i < n)//arr1下标小于arr1数组长度就继续放 { arr3[k] = arr1[i]; k++; i++; } } //进行打印: for (i = 0; i < n + m; i++) { printf("%d ", arr3[i]); } return 0; }
Realize the effect: