Blue Bridge - the number of columns to sort

Description of the problem:
  Given a length n, the number of columns, this number of columns arranged in large ascending order. 1 <= n <= 200
input format
  first line an integer n.
  The second line contains n integers, the number to be sorted, each of the integer absolute value of less than 10,000.
Output format
  output line, the number of columns in ascending order of the sorted output.
Sample input
. 5
. 8. 4. 3. 6. 9
Sample Output
34689
----------------

Analysis: as required question, enter a number n, then the number of inputs n, sort them. Very simple thing, my mind the first time in relation to think the easiest way is to define an array element will enter into an array, and then sort the output, this solution uses a common bubble sort, there are many more excellent other sorting method that can explore on their own practice.

#include<iostream>
using namespace std;
const int N = 200;

int main() {
	int n, i;
	int arr[N];    
	cin >> n;
	if (n >= 1 && n <= 200) {         //按照题目要求对数据规模进行限定
		for (i = 0; i < n; i++) {     //将输入的数存入数组
			cin >> arr[i]; 
		}
		for (i = 0; i < n; i++) {     //判断数组元素是否超出数据规定,如果超出,直接退出程序
			if (arr[i] <= -10000 || arr[i] >= 10000)
				exit(0);
		}
		for (int j = 0; j < n; j++) {        //冒泡排序
			for (i = 0; i < n - 1; i++) {
				int temp;
				if (arr[i] > arr[i + 1]) {
					temp = arr[i + 1];
					arr[i + 1] = arr[i];
					arr[i] = temp;
				}
			}
		}
	for (i = 0; i < n; i++) {    //输出排序后的元素
		cout << arr[i] << " ";
	}
	cout << endl;
	}
	return 0;
}

operation result:

 

 

Published 51 original articles · won praise 5 · Views 2016

Guess you like

Origin blog.csdn.net/MARS_098/article/details/103229616