杭电OJ_2019 数列有序!

嗯,做这题的整个心路历程中,发现了自己目前编程是多菜。。。。。。。。。。。。。八说了,冲冲冲
清一色都是各种墨迹的一批的判断语句、循环语句,这就是目前现状了,先认清自己,加油!!!

题目

数列有序!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 148624 Accepted Submission(s): 60423

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

代码(前面注释框起来的错的我就不删了,都是亲手敲的不是…)

#include <iostream>

using namespace std;

/*
int main() {
	int n, m;
	int arr[150] = {0};
	cin >> n >> m;
	while(!(n == 0 && m == 0)) {
		for(int i = 0; i < n; i++) {
			cin >> arr[i];
		if (n != 1) {
			bool sign = true;
			int count = 0;
			for (int i = 0; i < n; i++) {
				if (count >= 1) cout << " ";
				if( m < arr[i] && sign) {
					cout << m;
					sign = false;
					i--;
					count ++;
					continue;
				}
				cout << arr[i];
				count ++;
			}
			cout << endl;
		}
		if (n == 1 ) {
			if (arr[0] < m) {
				printf("%d %d\n", arr[0],m);
			}
			else {
				printf("%d %d\n", m,arr[0]);
			}
		}
		cin >> n >> m;  
	}
	return 0;
}
*/

int main() {
	int n, m;
	int aa[150];
	while (scanf("%d%d", &n, &m)) {
		if (n == 0 && m == 0) break;
		int sign = 999;
		for (int i = 0; i < n; i++) {
			cin >> aa[i];
		}
		for (int i = 0; i < n; i++) {
			if (aa[i] > m) {
				for (int j = n; j >= i+1; j--) {
					aa[j] = aa[j-1]; 	//后移一位 
				}
				aa[i] = m; 
				sign = 0;
				break;
			}
		}
		if (sign == 999) printf("%d %d\n", aa[0],m);	//只有一位数的情况 
		if (sign == 0) {
			for (int i = 0; i < n+1; i++) {
				if (i > 0) cout << " ";
				cout << aa[i];
			}
			cout << endl;
		}
	}
	return 0;
}
发布了32 篇原创文章 · 获赞 0 · 访问量 1179

猜你喜欢

转载自blog.csdn.net/qq_44296342/article/details/104119292