iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 7
0
自我挑戰組

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

7/150_11332 - Summing Digits、10252 - Common Permutation(Python)

本週解題數(5/7),題11332為昨日解的,題10252測資過評判錯

11332 - Summing Digits
10252 - Common Permutation(wrong


11332 - Summing Digits

解題思路:當字串累加後,長度大於1,繼續累加。

def inp():
    sinp=input()
    while(sinp!="0"):
        #print(sinp)
        print(dig(sinp))
        sinp=input()
解說 範例
讀取測資 test=766252547
def dig(st):
    coust=0
    if (len(st)>1):
        for i in range(len(st)):
            coust+=int(st[i])
        ans=dig(str(coust))
    else:
        ans=int(st)
    return ans
    
inp()
解說 範例
預設為0 7+6+6+2+5+2+5+4+7=44
如果長度大於一 4+4=8
ans=8
運算(字串長度)次數
字元整數化累加
不是
答案就是字串整數化

10252 - Common Permutation(wrong

解題思路:取二字串公因數,先取長度最短字串做因式分解

def inp():
    sinp_1=input()
    sinp_2=input()
    while(sinp_1!=""or sinp_2!=""):
        same(sinp_1,sinp_2)
        sinp_1=input()
        sinp_2=input()    

一次讀取二列測資

def same(sto,stw):
    sto,stw=min(sto,stw),max(sto,stw)
    same=[]
    samec={}
    for c in sto:
        try:
            samec[c]+=1
        except:
            samec[c]=1

長度最短當第一字串、長度最長當第二字串
相同字元初始化
因式分解初始化
從第一字串第一字算到最後一字
嘗試
因式分解[字]累加1
發生錯誤
因式分解[字]起始為1

    for s in samec:
        count=fin(s,samec[s],stw)
        if(count>0):
            for i in range(count):
                same.append(s)
    same.sort()
    for i in range(len(same)):
        print(same[i],end="")
    print("")

從因式分解第一字算到最後一字
第一字串和第二字串的共同字有幾個
是否大於0?

相同字元累加該次數
進行字元A~Z排序
輸出相同字元

def fin(st,couli,sta):
    count=0
    for s in sta:
        if(s==st):
            count+=1
        if(count==couli):
            return couli
    return count
    
inp()

次數初始化
從第二字串第一字計算到最後一字
請問有找到嗎?
是,次數累加
請問有超過上限嗎?
是,輸出上限值
輸出次數


上一篇
6/150_UVa100、UVa10222
下一篇
8/150_第八天新的里程碑
系列文
程式設計師大冒險115
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言