python ACM ,持续更新中。。。。。。。。。。。。

1, C语言实验——图形输出(字符常量练习):  http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1112

python中用print总是会出现回车空格,非常讨厌,然而,Python 3.0下print已经从语句变为函数了,我用的使python2.7.4,所以呢,还不行。。。。。但是我们可以用其他方法:

import sys
sys.stdout.write("abc")
sys.stdout.write("def")
#!/usr/bin/env python
#coding:utf8

import sys

for i in range(1, 6+1):
    for j in range(1, i+1):
        sys.stdout.write("#")
    sys.stdout.write('\n')

2, C语言实验——交换两个整数的值(顺序结构):http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1115

pathon交换两个值非常简单哦,看下面:

#!/usr/bin/env python
#coding:utf8

x, y = raw_input().split()

x , y = y, x
print x,y

3, C语言实验——for循环打印图形(循环结构): http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1121

也许使因为刚使用,所以代码写的有点长,以后看看能不能精简些,欢迎拍砖

#!/usr/bin/env python
#coding:utf8

import sys

for i in range(1,7+1):
    if i<4:
        for j in range(1,5-i):
            sys.stdout.write(" ")
        for j in range(1,2*i):
            sys.stdout.write("*")
        sys.stdout.write("\n")
    elif i==4:
        for j in range(1,7+1):
            sys.stdout.write("*")
        sys.stdout.write("\n")
    else:
        for j in range(1,i-4+1):
            sys.stdout.write(" ")
        for j in range(1,6-2*(i-5)):
            sys.stdout.write("*")
        sys.stdout.write("\n")
View Code

 4, IBM Minus One : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1053

#!/usr/bin/env python
#coding:utf-8

import sys

t = int(raw_input())
cases = 0

while t>0:
    t = t-1
    cases = cases+1
    line = raw_input()
    res = []
    for ch in line:
        if ch=='Z':
            res.append('A')
        else:
            res.append(chr(ord(ch)+1))
    print "String #%d" % cases
    for ch in res:
        sys.stdout.write("%c" % ch)
    print '\n'
View Code

5, 1574 组合数的计算: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1574

#!/usr/bin/env python
# coding=utf-8

import math

t = int(raw_input())

while t>0:
    if t==0:
        break
    t = t-1
    a,b=raw_input().split()
    if int(a)<int(b):
        print "0"
        continue
    print long(math.factorial(int(a)))/long(math.factorial(int(a)-int(b)))/long(math.factorial(int(b)))
View Code

6, 1174 C语言实验——打印菱形: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1174

#!/usr/bin/env python
# coding=utf-8

import sys

while True:
    n = int(raw_input())
    if n==1:
        print "*"
        continue
    for i in range(1,n):
        for j in range(1,n+1-i):
            sys.stdout.write(" ")
        for j in range(1,2*i):
            sys.stdout.write("*")
        print "\r"
    for i in range(1,2*n):
        sys.stdout.write("*")
    print "\r"
    for i in range(1,n):
        for j in range(1,i+1):
            sys.stdout.write(" ")
        for j in range(1,2*(n-i)):
            sys.stdout.write("*")
        print "\r"
View Code

7, 2543 整除 : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2543

#!/usr/bin/env python
#coding:utf-8

while True:
    n = int(raw_input())
    ans = n/5+n/6+n/8-n/30-n/40-n/24+n/120
    print ans
View Code

8, 1064 Binary Numbers : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1064

#!/usr/bin/env python
#coding:utf-8

import sys

t = int(raw_input())

while t>0:
    t = t-1
    n = int(raw_input())
    cnt = 0
    while n!=0:
        if n&1:
            sys.stdout.write("%d " % cnt)
        cnt=cnt+1
        n>>=1
    print "\r"
View Code

9, 1791 集合相等问题 : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1791

#!/usr/bin/env python
#coding:utf-8

while True:
    n = int(raw_input())
    a=[]
    b=[]
    line1=raw_input()
    line2=raw_input()
    for x in line1.split():
        a.append(int(x))
    for x in line2.split():
        b.append(int(x))
    a.sort()
    b.sort()
    if a==b:
        print "YES"
    else:
        print "NO"
View Code

10, 等值数目 : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1227

#!/usr/bin/env python
#coding:utf-8

while True:
    n,m=raw_input().split()
    a=[]
    b=[]
    line1=raw_input()
    line2=raw_input()
    for x in line1.split():
        a.append(int(x))
    for x in line2.split():
        b.append(int(x))
    a.sort()
    b.sort()
    cnt1=0
    cnt2=0
    ans=0
    while cnt1<int(n) and cnt2<int(m):
        if(a[cnt1]==b[cnt2]):
            ans=ans+1
            cnt1=cnt1+1
            cnt2=cnt2+1
        elif(a[cnt1]>b[cnt2]):
            cnt2=cnt2+1
        elif(a[cnt1]<b[cnt2]):
            cnt1=cnt1+1
    print ans
View Code

11, C语言实验——数日子 : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1182

#!/usr/bin/env python
#coding:utf-8

from datetime import *
import time

n=int(raw_input())

while n>0:
    n=n-1
    y,m,d=raw_input().split()
    now=date(int(y),int(m),int(d))
    a=int(now.strftime("%j"))
    print a
View Code

具体的时间函数的使用可参考:http://www.cnblogs.com/lhj588/archive/2012/04/23/2466653.html

 12, 2565 区间之和 : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2565

#!/usr/bin/env python
# coding=utf-8

n = int(raw_input())

a=[]
lines=raw_input()
for x in lines.split():
    a.append(int(x))
l,r=raw_input().split()
b=[]
b.append(a[0])
for i in range(1,n):
    b.append(b[i-1]+a[i])
if int(l)==1:
    print b[int(r)-1]
else:
    print b[int(r)-1]-b[int(l)-2]
View Code

 13, C语言实验——大小写转换: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1168

#!/usr/bin/env python
# coding=utf-8

import sys
import string

lines=raw_input()
for ch in lines:
    if ch.isupper():
        sys.stdout.write("%c" % ch.lower())
    elif ch.islower():
        sys.stdout.write("%c" % ch.upper())
    else:
        sys.stdout.write("%c" % ch)
print "\r"
View Code

更多的字符串操作:http://blog.csdn.net/liuxincumt/article/details/7945337

 14, 1576 魔幻数字47 : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1576

#!/usr/bin/env python
# coding=utf-8

num=[47,147,247,347,447,547,647,747,847,947,1047,1147,1247,1347,1447,1547,1647,1747,1847,1947,2047,2147,2247,2347,2447,2547,2647,2747,2847,2947,3047,3147,3247,3347,3447,3547,3647,3747,3847,3947,4047,4147,4247,4347,4447,4547,4647,4747,4847,4947,5047,5147,5247,5347,5447,5547,5647,5747,5847,5947,6047,6147,6247,6347,6447,6547,6647,6747,6847,6947,7047,7147,7247,7347,7447,7547,7647,7747,7847,7947,8047,8147,8247,8347,8447,8547,8647,8747,8847,8947,9047,9147,9247,9347,9447,9547,9647,9747,9847,9947];

n = int(raw_input())

while n>0:
    n=n-1
    a,b=raw_input().split()
    if int(a)>int(b):  #注意大小的比较
        a,b=b,a
    flag=0
    for x in range(0,100):
        if num[x]>=int(a) and num[x]<=int(b):
            print num[x]
            flag=1
    if flag==0:
        print "NONE"
View Code

15, 1595 选夫婿2: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1595

#!/usr/bin/env python
# coding=utf-8

while True:
    n = int(raw_input())
    dic={}
    for i in range(0,n):
        a,b=raw_input().split()
        dic[a]=int(b)
#    sorted(dic.items(),key=lambda dic:dic[1])
    a,b=raw_input().split()
    for x in dic.keys():
        if dic[x]<int(a) or dic[x]>int(b):
            del dic[x]
    ans=sorted(dic.items(),key=lambda dic:dic[1])
#    for x in dic.keys():
#        print "%s %d" % (x,dic[x]) 
    name=[]
    hei=[]
    if ans==[]:
        print "No"
        continue
    name,hei=zip(*ans)
    length=len(name)
    for i in range(0,length):
        print "%s %d" % (name[i],hei[i])
View Code

 

16, 1573 排排站: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1573

#!/usr/bin/env python
# coding=utf-8

t = int(raw_input())
cases=0

while t>0:
    t=t-1
    cases=cases+1
    n=int(raw_input())
    num=[]
    for x in raw_input().split():
        num.append(float(x))
    me=num[n-1]
    num.sort()
    ans=0
    for i in range(0,n):
        if me>=num[i]:
            ans=ans+1
        else:
            break
#    if ans!=n and ans!=1:
#        ans=ans-1
    print "Case #%d: %d" % (cases,ans)
View Code

 map排序详细介绍:http://blog.csdn.net/business122/article/details/7537014

原文地址:https://www.cnblogs.com/jackge/p/3115418.html