算法竞赛入门经典(1,2章的python实现)

可以说几乎没有什么难题,但是有几个地方还是值得注意.关乎一些编程的技巧.

首先对于最大最小值,注意最好还是用第一个数组里的值作为初始值,避免了人为的假设最大值和最小值.

对于浮点数的运算,一定要注意误差,比如转换成整数的时候要采用四舍五入的办法比如调用round函数或者int(f+0.5)。

要求四舍五入的时候注意要多求一位.

用好双端队列.

def average(a,b,c):
    print('%.3f'%((a+b+c)/3))
def temperature(f):
    print('%.3f'%(5*(f-32)/9))
def sum(n):
    print('%d'%((1+n)*n/2))
from math import sin,cos,pi,sqrt
def Sin(n):
    print(sin(n/180*pi))
def discount(m):
    n = m*95
    print (n*0.85 if n>=300 else n)
def triangle(_a,_b,_c):
    L = [_a,_b,_c]
    L.sort()
    a,b,c = L[0],L[1],L[2]
    if(a+b<c):
        print('not a triangle')
    else:
        print ('yes' if a*a+b*b == c*c else 'no')
def year(n):
    if(n%100==0):#世纪年
        n //= 100
    print('yes' if n%4==0 else 'no')
def test():
    a = False
    b = False
    c = True
    print(a&b|c)
def aabb():
    for a in range(1,10):
        for b in range(0,10):
            n = a*1100+b*11
            test = int(sqrt(n)+0.5)#防止浮点数误差,重要的小trick
            if(test*test == n):
                print(n)
def three_n(n):
    i = 0
    while(n!=1):
        if(n%2==1):n = 3*n+1
        else:n /= 2
        i += 1
    print(i)
def appromix():#
    result = 0.0
    a = 1
    b = 1
    while True:
        t = a/b
        result += t
        b += 2
        a *= -1
        if(abs(a/b)<=10**-6):
            break
    print(result)
#计算阶乘之和
def sum_facorial(n):
    MOD = 10**6
    def get_factorail(m):
        res = 1
        for i in range(1,m+1):
            res *= i;
        return res%MOD
    sum = 0
    for i in range(1,n+1):
        sum = (sum + get_factorail(i))%MOD
    print(sum)
import sys  
###注意输入输出的格式
def data_collect():
    INF = 10**8
    while(True):
        n = int(input())
        print(n)
        if(n==0):break
        min = INF#避免人为的设置最大最小值
        max = -INF
        count = 0
        sum = 0
        L = map(int,sys.stdin.readline().split())  
        for i in L:
            if(min>i):min = i
            if(max<i):max = i
            count += 1
            sum += i
        print(min,max,'%.3f'%(sum/count))
#水仙花数
def daffodil():
    for i in range(100,1000):
        a = (i//100)%10
        b = (i//10)%10
        c = i%10
        if(a**3+b**3+c**3 == i):print(i)
#韩信
def hanxin(a,b,c):
    min = 10
    max = 100
    for k in range((10-c)//7+1,(100-c)//7+1):
        n = 7*k + c
        if((n%5==b)&(n%3==a)):
            print(n)
            return
    print('No answer')
#倒三角
def reverse_triangle(n):
    row = 2*n-1
    for k,i in enumerate(range(row,0,-2)):
        print(k*' ','*'*i)

def subsequence(n,m):
    sum = 0.0
    for i in range(m,n-1,-1):
        sum += 1/i**2
    print('%.5f'%sum)
#输出固定精度的小数
def decimal(a,b,c):
    ans1 = '';ans2 = '.'
    count = 0;r = 0
    if(a>=b):
        t = a//b
        r = a%b
        a = r
        ans1 += str(t)
    while(count<=c):
            t = a//b
            r = ((a%b)*10)//b
            a = (a%b)*10
            ans2 += str(r)
            count += 1
    ans = round(float(ans1+ans2), c)
    print(ans)
from collections import deque

###求abc:def:ghi = 1:2:3
def Test (a,b,L):
    a1 = (a//100)%10;a2 = (a//10)%10;a3 = (a)%10
    b1 = (b//100)%10;b2 = (b//10)%10;b3 = (b)%10
    T = []
    for i in [a1,a2,a3,b1,b2,b3]:
        if((i in L) and (i not in T)):
            T.append(i)
        else:
            return
    print(int(b/3),a,b)

def do_permutation(L,abc):
    n = len(L)
    for k in range(0,n):
        i = L.popleft()
        sum =  abc*10 + i
        if(sum<100):
            do_permutation(L,sum)
        elif (sum<329):
            de_f = sum*2
            gh_i = sum*3
            Test(de_f,gh_i,L)
        L.append(i)

def permutation():
    L = deque([i for i in range(1,10)])
    print(L)
    do_permutation(L,0)

你可能感兴趣的:(算法竞赛入门经典(1,2章的python实现))