首页 > 试题广场 >

整数奇偶排序

[编程题]整数奇偶排序
  • 热度指数:23953 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到小排列; 2.然后输出其中的偶数,并按从小到大排列。

输入描述:
任意排序的10个整数(0~100),彼此以空格分隔。


输出描述:
可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。

1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现;
2. 输入数据随机,有可能相等。
示例1

输入

4 7 3 13 11 12 0 47 34 98

输出

47 13 11 7 3 0 4 12 34 98
while True:
    try:
        inp=list(map(int,input().strip().split(' ')))
        list1=[]
        list2=[]
        for i in inp:
            if i%2==0:
                list2.append(i)
            else:
                list1.append(i)
        list1=sorted(list1,reverse=True)
        list2=sorted(list2)
        list1=list(map(str,list1))
        list2=list(map(str,list2))
        result=' '.join(list1+list2)
        print(result)
    except:
        break
发表于 2019-08-09 14:39:03 回复(0)
while True:
    try:
        digitList = list(map(int,input().split()))
        odd = list(filter(lambda x:x%2==1,digitList))
        even = list(filter(lambda x:x%2==0,digitList))
        odd.sort(reverse=True)
        even.sort()
        print(" ".join(map(str,odd)),end=" ")
        print(" ".join(map(str,even)))
    except Exception:
        break
编辑于 2018-10-01 20:24:57 回复(0)

python两行代码搞定,就是这么b



while True:
    try:
        a=list(map(int,input().split()))
        print(" ".join(map(str,sorted(filter(lambda c:c%2==1,a),reverse=True)+sorted(filter(lambda c:c%2==0,a)))))
    except:
        break
发表于 2017-10-06 15:35:46 回复(3)
try:
    while 1:
        L = map(int, raw_input().split())
        print ' '.join(map(str, sorted(list(filter(lambda x:x % 2 != 0, L)),reverse=True) + sorted(list(filter(lambda x:x % 2 == 0, L)))))
except:
    pass

发表于 2016-12-26 19:41:50 回复(0)