iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 6
0
自我挑戰組

程式設計師大冒險系列 第 6

6/150_UVa100、UVa10222

設定條件函數,求運算次數,這題演算法很重要。另一題則是密碼學,學好這題以後就可以寫祕密日記。

  • 100 -The 3n 1 problem(time out
  • 10222 - Decode the Mad man(wrong anser

100 -The 3n 1 problem(time out

#宣告
global cach #全域變數
cach={1:1} #紀錄該數字會運算次數,之後可以節省運算

def main():
    sinput=input()
    while (sinput!=""):
        num_1,num_2=map(int,sinput.split())
        chobig(num_1,num_2) #進入運算
        sinput=input()

讀取輸入

def chobig(o,t):
    biged=max(o,t)
    bigst=min(o,t)
    big=0
    
    #比較次數最大值
    for i in range(bigst,biged+1):
        big=max(tre(i),big)
    print(o,t,big)

測資一和測資二選擇最大者
測資一和測資二選擇最小者
從最小數計算到最大數
此次運算次次和目前運算次數冠軍選擇最大者
輸出測資一、測資二、運算次數最大者

def tre(num):
    count=cach.get(num)
    orin=num
    
    if(count==None):
        if (num%2==0):
            num=int(num/2)
        else:
            num=num*3+1
        count=tre(num)+1
        cach[orin]=count
    return count
    
main()  

運算次數計算函數
搜尋字典有沒有這個數字
另外存放原始數字
如果字典沒有
是否整除二
除以二

乘以3倍+1


10222 - Decode the Mad man(wrong anser  

import string
def inp():
    sinp=input().strip()
    cha(sinp)
        
def cha(st):
    row1="qwertyuiop[]"
    row2="asdfghjkl;'"
    row3="zxcvbnm,./"
    no=("q","w","a","s","z","x")
    for i in range(len(st)):
        thst=st[i]
        if (st[i]==" "):
            print(end=" ")
        else:
            thst=thst.lower()
        if thst not in no:
            ifrow(row1,thst)
            ifrow(row2,thst)
            ifrow(row3,thst)

列1到列3請參照鍵盤
no列表示不存在範圍
測資從第一次解碼到最後一字
取出一字
是否為空白
是,輸出空白
否,將字統一降階為小寫
存在於範圍內嗎?
檢測第一列
檢測第二列
檢測第三列

def ifrow(row,x):
    for i in range(len(row)):
        if (x == row[i]):
            print(row[(i-2)],end="")
      
inp()

將字元取代為鍵盤左二字
從鍵盤第一字計算到最後一字
請問有符合嗎?
有,請輸出左二字


總結

距離本月目標四十題還有一段差距,不過十月是求職淡季,月中後會有很多時間刷題。
希望至少達成這週的七題
今天解三題,一題過;一題超時;一題全錯。


上一篇
5/150_用字典(dict)減少運算次數
下一篇
7/150_11332 - Summing Digits、10252 - Common Permutation(Python)
系列文
程式設計師大冒險115
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言