题解 | #输入整型数组和排序标识,对其元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
http://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
#include <iostream>
using namespace std;
int main()
{
int a;
while (cin >> a)
{
int b[1000] = { 0 };
for (int i = 0; i < a; i++)
{
cin >> b[i] ;
}
for (int i = 0; i < a - 1; i++)
{
for (int j = i + 1; j < a; j++)
if (b[i] > b[j])
{
int c;
c = b[i];
b[i] = b[j];
b[j] = c;
}
}
int d;
cin >> d;
if(d==0)
{
for (int i = 0; i < a; i++)
cout << b[i] <<" ";
}
if(d==1)
{
for(int i=a-1;i>=0;i--)
{
cout << b[i] << " ";
}
}
}
return(0);
}
using namespace std;
int main()
{
int a;
while (cin >> a)
{
int b[1000] = { 0 };
for (int i = 0; i < a; i++)
{
cin >> b[i] ;
}
for (int i = 0; i < a - 1; i++)
{
for (int j = i + 1; j < a; j++)
if (b[i] > b[j])
{
int c;
c = b[i];
b[i] = b[j];
b[j] = c;
}
}
int d;
cin >> d;
if(d==0)
{
for (int i = 0; i < a; i++)
cout << b[i] <<" ";
}
if(d==1)
{
for(int i=a-1;i>=0;i--)
{
cout << b[i] << " ";
}
}
}
return(0);
}