C language daily question

What I am sharing today is a topic on Niuke.com, and the links
are merged in an orderly sequence below

insert image description here
There are many ways to do this question. The easiest way is to merge them together, and then use sorting. Today, I will use the most efficient method. The idea is to compare the first items of the two arrays, output the smaller one first, and then output the next array. The subscript moves backwards, and then compares. If the other array is smaller, then output that one first, and then the subscript of that array moves backwards. In this way, the two arrays can be combined for output.

#include<stdio.h>
int main()
{
    
    
	int arr1[1000];
	int arr2[1000];
	int n = 0;
	int m = 0;
	scanf("%d %d", &n, &m);
	int i = 0;
	for (i = 0; i < n; i++)
	{
    
    
		scanf("%d ", &arr1[i]);
	}
	for (i = 0; i < m; i++)
	{
    
    
		scanf("%d ", &arr2[i]);
	}
	int j = 0;
	i = 0;
	while (i < n && j < m)
	{
    
    
		if (arr1[i] > arr2[j])
		{
    
    
			printf("%d ", arr2[j]);
			j++;
		}
		else
		{
    
    
			printf("%d ", arr1[i]);
			i++;

		}
	}
	if (i == n)
	{
    
    
		for (; j < m; j++)
		{
    
    
			printf("%d ", arr2[j]);
		}
	}
	else
	{
    
    
		for (; i < n; i++)
		{
    
    
			printf("%d ", arr1[i]);
		}
	}
	return 0;
}

This is today's code sharing, mainly because I think this idea is okay, so I shared it. The quality of the article is not particularly good, thank you! ! !

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/132050615
Recommended