对于数组中的两个下标i,j(1<=i,j<=n),如果为奇数,就可以交换和。
第一行一个整数n;
第二行n个整数,表示数组,每两个数字之间用一个空格分隔。
输入保证。
n个整数,每两个整数之间用一个空格分隔,表示得到的字典序最小的数组。
4 7 3 5 1
7 3 5 1
10 53941 38641 31525 75864 29026 12199 83522 58200 64784 80987
12199 29026 31525 38641 53941 58200 64784 75864 80987 83522
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextLong(); } demo1(arr); return; } } public static void demo1(long[] arr) { //奇数数量 int oddSum = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 1) { oddSum++; } } //如果全是偶数或者全是奇数则无法排序,直接输出 if (oddSum == 0 || oddSum == arr.length) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } return; } //如果至少存在一对奇偶数,则可以将序列全部从小到大排列 Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
n = int(input()) arr = list(map(int, input().strip().split(' '))) allOdd, allEven = 0, 0 for a in arr: if a % 2 == 1: allOdd += 1 elif a % 2 == 0: allEven += 1 if (allOdd == n)&nbs***bsp;(allEven == n): # 如果数组中全是奇数或全是偶数,则无法执行交换操作,直接输出数组 arr = [str(i) for i in arr] print(' '.join(arr).strip()) else: # 否则就是把数组排序好的状态 arr = sorted(arr) arr = [str(i) for i in arr] print(' '.join(arr).strip())
def solve(nums): if len(nums) == 0: return [] odd = 0 for i in range(len(nums)): if nums[i] % 2 == 0: odd += 1 if odd == 0&nbs***bsp;odd == len(nums): return nums else: return sorted(nums) n = int(input()) nums = list(map(int,input().split())) res = solve(nums) for i in range(n): print(res[i],end = ' ')
#include<iostream> #include<algorithm> using namespace std; int main(){ int n; cin>>n; long int a[n]; int odd_num = 0; for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++){ if(a[i]&1) odd_num += 1; } if(odd_num==0 || odd_num==n){ for(int i=0;i<n-1;i++) cout<<a[i]<<" "; cout<<a[n-1]<<endl; } else{ sort(a,a+n); for(int i=0;i<n-1;i++) cout<<a[i]<<" "; cout<<a[n-1]<<endl; } }
var n = parseInt(readline()) var arr = readline().split(" ") var ji = arr.every(item=>Number(item)%2) //判断是否全是奇数 var ou = arr.every(item=>Number(item)%2===0) //判断是否全是偶数 if(ji||ou){ console.log(arr.join(" ")) }else{ arr.sort((a,b)=>a-b) console.log(arr.join(" ")) }怎么简洁怎么来
package main import( "fmt" "sort") func solve(nums []int) []int{ if len(nums)==0 {return nums } odd:=0 for _,v:= range nums{ if v%2==0{odd++} } if odd==len(nums) || odd==0{ return nums }else{ sort.Ints(nums) return nums } } func main(){ count:=0 fmt.Scan(&count) slice:=make([]int,0) for i:=0;i<count;i++{ elem:=0 fmt.Scan(&elem) slice=append(slice,elem) } res:=solve(slice) for _,v:=range res{ fmt.Printf("%d ",v) } }
def sort_f(alist): odd ,even = 0,0 for i in alist: if i%2 == 0: odd+=1 else: even+=1 if odd != 0 and even != 0:# 有奇数有偶数,直接排序 return sorted(alist) return alist n = int(input()) b = list(map(int,input().split())) for i in sort_f(b): print(i,end=' ')
n = int(input()) data = [int(a) for a in input().split()] for i in range(0,n): if data[i]%2 == 0: for j in range(i+1,n): if data[j]%2 != 0: data.sort() else: for j in range(i+1,n): if data[j]%2 == 0: data.sort() print(data[i],end=" ")
a = input() b = list(map(lambda x:int(x),input().split())) c = list(map(lambda x:x%2,b)) if sum(c) != len(c) and sum(c): b.sort() print(' '.join(list(map(lambda x:str(x),b))))好神奇
n = int(input()) data = list(map(int, input().split())) odd, even = False, False for i in data: if i & 1: odd = True else: even = True if not odd&nbs***bsp;not even: print(" ".join(str(i) for i in data)) else: print(" ".join(str(i) for i in sorted(data)))
const n = readline(); const line = readline(); let lines = line.split(' '); for (let i = 0; i < n; i++) { lines[i] = parseInt(lines[i]); }; const odd = lines.some(item => ( (item % 2) == 1 )); const even = lines.some(item => ( (item % 2) == 0 )) if (odd && even) { lines.sort((a, b) => (a - b)); } let newlines = lines.join(' '); console.log(newlines);