2018-03-27-比大小

1、问题描述:


思路:


代码:自己的代码(运行了80%后,时间超限 ( Time Limit Exceeded (TLE) )

import itertools
if __name__=='__main__':
    n=int(raw_input())
    s="abcdefghijkl"
    ret =itertools.permutations(s)
    count=0
    res=[]
    j=0
    for i in range(n):
        a=raw_input()
        res.append(a)
    for i in ret:
        count+=1
        if i in res and j<n:
            res[res.index(i)]=count
            j=j-1
            count=0
        if j==n:
            break
    for i in res:
        print i

2、知识点补充:

库链接:https://docs.python.org/2.7/library/itertools.html

itertools模块:itertools — Functions creating iterators for efficient looping

The following module functions all construct and return iterators. Some provide streams of infinite length, so they should only be accessed by functions or loops that truncate the stream.

itertools. permutations ( iterable [r ] )

Return successive r length permutations of elements in the iterable.

If r is not specified or is None, then r defaults to the length of the iterable and all possible full-length permutations are generated.

Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.

Roughly equivalent to:

def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = range(n)
    cycles = range(n, n-r, -1)
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

The code for permutations() can be also expressed as a subsequence of product(), filtered to exclude entries with repeated elements (those from the same position in the input pool):

def permutations(iterable, r=None):
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    for indices in product(range(n), repeat=r):
        if len(set(indices)) == r:
            yield tuple(pool[i] for i in indices)

The number of items returned is n! / (n-r)! when 0 <= r <= n or zero when r > n.

New in version 2.6.


3、别人家的代码:

3.1、

n=int(raw_input())
a=''
b=[]
def jiecheng(n):

    res=1
    for i in range(1,n+1):
        res=res*i
    return res
for i in range(n):
    a=raw_input()
    c=[]
    b=[]
    for k in range(12):
        b.append(a[k])
        c.append(a[k])

    #b='abcdefghijkl'
    res=1
    l=0
    q=[]
    for j in range(12):
        c=sorted(c)
        #print(c)
        l=0
        for p in q:
            if ord(p)<ord(b[j]) and ord(p)>ord(c[0]):
                l+=1
        res+=(ord(b[j])-ord(c[0])-l)*jiecheng(11-j)
        #print(res)

        c.pop(c.index(b[j]))
        q.append(b[j])
    print res

3.2、

import math
numbers = int(raw_input())
for m in range(numbers):
	s = (raw_input())
	rlt = 1
	sdi = 0
	for i in range(12):
		t = s[i]
		s0 = s[i:]
		n = 0
		for j in s0:
			if(t>j):
				n += 1
		rlt += math.factorial(11-i)*n
	print rlt
3.3、

基础知识备注:

1、用ASCII转换函数ord(),当然也可以用最简单的字典,自己指定大小,eg:key_list = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11}

2、math包中的求阶乘函数:

import math

math.factorial(11 - j)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值