题解 | #输入整型数组和排序标识,对其元素按升或降进行排序
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
#include <stdio.h>
#include <string.h>
int main() {
int num;
int arr[1001]={0};
int mark;
int temp;
while (scanf("%d", &num) != EOF) { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld") to
for(int i=0;i<num;i++){
scanf("%d ",&arr[i]);
}
scanf("\n%d",&mark);
for(int i=0;i<num-1;i++){
for(int j=i+1;j<num;j++){
if(mark==0){
if(arr[i]>arr[j]){
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}else{
if(arr[i]<arr[j]){
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
}
}
for(int i=0;i<num;i++){
printf("%d ",arr[i]);
}
}
return 0;
}
文远知行公司福利 544人发布
