HDU 2019

HDU 2019

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output
对于每个测试实例,输出插入新的元素后的数列。

Sample Input
3 3
1 2 4
0 0

Sample Output
1 2 3 4

问题分析:
排序问题

程序说明:
暴力解决,直接加一个排序函数就可以,要注意输出格式,就是最后结束不能再输出空行。

AC的c++代码如下:

#include<iostream>
using namespace std;
void sort(int  a[], const int n)  
{
	int i, j;
	int temp;          
	for (i = 1; i < n+1; i++)
	{
		for (j = 0; j <= n  - i; j++)
			if (a[j]> a[j + 1])
			{
				temp = a[j];  
				a[j] = a[j + 1];
				a[j + 1] = temp;
			}
	}
}

int main()
{
	int a[100];
	int n, m;
	while (cin >> n >> m)
	{
		if (n == 0 && m == 0)
			break;
		else
		{
			for (int i = 0; i < n; i++)
				cin >> a[i];
			a[n] = m;
			sort(a, n);
			for (int i = 0; i < n ; i++)
				cout << a[i] << " ";
			cout << a[n];
			cout << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43983336/article/details/85005384
今日推荐