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>