輸入整型數組和排序標識,對其元素按照升序或降序進行排序

描述

輸入整型數組和排序標識,對其元素按照升序或降序進行排序

接口說明

原型:

void sortIntegerArray(Integer[] pIntegerArray, int iSortFlag);

輸入參數:

    Integer[] pIntegerArray:整型數組

int  iSortFlag:排序標識:0表示按升序,1表示按降序

輸出參數:

    無

返回值:

    void


知識點 循環,排序,數組
運行時間限制 10M
內存限制 128
輸入

1、輸入需要輸入的整型數個數

2、輸入數組組

3、輸入排序標識


輸出

輸出排好序的數字

最後一個無空格

如下例子


樣例輸入 8 1 2 4 9 3 55 64 25 0
樣例輸出 1 2 3 4 9 25 55 64
#include <iostream>
#include <string>
#include <vector>
#include<iomanip>
#include <algorithm>
using namespace std;

bool cmp1(const int &a,const int &b)
{
	return(a<b);
}
bool cmp2(const int &a,const int &b)
{
	return(a>b);
}
int main()
{
	int n;
	cin>>n;
	vector<int> a;
	int value;
	for (int i=0;i<n;i++)
	{
		cin>>value;
		a.push_back(value);
	}
	int flag;
	cin>>flag;
	if (flag==0)
	{
		sort(a.begin(),a.end(),cmp1);
	} 
	else
	{
		sort(a.begin(),a.end(),cmp2);
	}
	for (int i=0;i<n;i++)
	{
		if (i!=n-1)
		{
			cout<<a.at(i)<<" ";
		}	
		else
		{
			cout<<a.at(i);
		}
		
	}
	cout<<endl;
	return 0;
}





發佈了35 篇原創文章 · 獲贊 9 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章