Python--ZOJ 1216、1240、1241、1242

ZOJ  1216、1240、1241、1242


这几道题都属于简单的找规律或者是直接用数学公式求解的题目,只需要带公式计算即可,所以一起写出来了。

ZOJ1216

    这是一个卡片放置的问题,若将卡片放在桌子边缘不至于掉下去那么卡片的边缘最远与桌子的边缘相差多少?也就是说几张卡片叠在一起重心要刚好在桌子边缘上,这种问题直接找规律套公式就好了。通过给出的几组测试用例可以看出距离S(i)=S(i-1)+1/(2*i),所以代码如下:

<span style="font-family:SimSun;font-size:14px;">#2015-02-18
# -*- coding: utf-8 -*-

print '# Cards  Overhang'
while True:
    try:
        n = int(input())
        count = 0
        for i in range(1,n+1):
            count += 1.0/(2*i)
        print '%5d     %.3f'%(n,count)
        
    except EOFError:
        break</span>

ZOJ1240

这是一种简单的字符位移,跟凯撒密码差不多,需要注意的一点是当字符位移到最后一位Z的时候要回到A,不能越界。

<span style="font-family:SimSun;font-size:14px;">#2015-02-18
# -*- coding: utf-8 -*-

n = input()
count = 1
while count <= n:
    string = raw_input()
    newstring = ''
    for i in string:
        if ord(i)+1<=90:
            newstring += chr(ord(i)+1)
        else:
            newstring += chr(ord(i)-25)
    print 'String #%d'%count
    print newstring
    print '\n',
    count += 1</span>

ZOJ1241

这道题就是勾股定理啦,给你一组数,判断能否构成直角三角形。这里主要是各种情况的判断,当a、b、c分别为-1时应该具体讨论,详细的还是看代码吧,要注意输出结果的等号两侧是有空格的。

<span style="font-family:SimSun;font-size:14px;">#2015-02-18
# -*- coding: utf-8 -*-
import math

count = 1
while True:
    d = raw_input().split()
    a = float(d[0])
    b = float(d[1])
    c = float(d[2])
    if a==0 and b==0 and c==0:
        break
    print 'Triangle #%d'%count
    if a<0 and b<0 and c<0:
        print 'Impossible.'
    elif a==-1:
        if c<=b or b <=0 or c<=0:
            print 'Impossible.'
        else:
            a = math.sqrt(c**2-b**2)
            print 'a = %.3f'%a
    elif b==-1:
        if c<=a or c<=0 or a <=0:
            print 'Impossible.'
        else:
            b = math.sqrt(c**2-a**2)
            print 'b = %.3f'%b
    elif c==-1:
        if a<=0 or b <=0:
            print 'Impossible.'
        else:
            c = math.sqrt(a**2+b**2)
            print 'c = %.3f'%c
    else:
        print 'Impossible.'
    if a!=0 or b!=0 or c!=0:
        print '\n',
    count += 1</span>


ZOJ1242

    这题是根据C14的衰变计算年代的,也是套公式就完了,从死亡开始y年,生物体中的C14量为:a(y) = a0 * 2-y/h ,其中,a0 = 810 , h=5730是半衰期,那么y=log(a0/a(y))*h。具体的信息也可以百度到。这里还涉及到最后的四舍五入的问题,具体的操作写在代码注释里。

<span style="font-family:SimSun;font-size:14px;">#2015-02-19
# -*- coding: utf-8 -*-
import math

a = 810
dec = 5730
count = 1
while True:
    l = raw_input().split()
    w = float(l[0])
    d = float(l[1])
    if w == 0 and d == 0:  #输入为0时退出
        break
    year = dec*math.log((a*w/d))/math.log(2)
    if year >= 10000:
        year = int(round(year/1000)*1000)    #round()函数,四舍五入,大于10000千位近似,小于10000,百位近似
    else:
        year = int(round(year/100)*100)
    print 'Sample #%d'%count
    print 'The approximate age is %d years.'%year
    count += 1
    print '\n',
</span>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值