BAEKJOON : 11720, 10809, 2675, 1157, 2908, 5622


No. 11720


1. Problem

2. My Solution
  • eval関数を使用して文字列式
  • を実行します.
    import sys
    
    n = sys.stdin.readline().strip()
    result = eval('+'.join(sys.stdin.readline().strip()))
    
    print(result)

    No. 10809


    1. Problem

    2. My Solution
    ord()関数を用いて
  • 文字a~zの範囲に設定
  • index()関数を実行するときに該当する要素がない場合は、
  • 文を使用してValueError generator->try-除外
    import sys
    
    s = sys.stdin.readline().strip()
    result = []
    
    for i in range(ord('a'), ord('z')+1):
        try:
            result.append(s.index(chr(i)))
        except:
            result.append(-1)
    
    for i in range(len(result)):
        print(result[i], end=' ')
  • その他の方法
  • import sys
    
    word = sys.stdin.readline().strip()
    alphabet = [-1] * 26
    
    for i in word:
        alphabet[ord(i)-97] = word.index(i)
    
    print(' '.join(map(str,alphabet)))
    3. Learned
  • ord()-文字をAskyコード
  • に変換
  • chr()-Askyコードを文字
  • に変換
  • 異常発生時は除外文
  • を用いる.

    No. 2675


    1. Problem

    2. My Solution
    import sys
    
    n = int(sys.stdin.readline().strip())
    
    for i in range(n):
        rep, str = sys.stdin.readline().strip().split()
        result = ""
       
        for j in range(len(str)):
            result += (str[j]* int(rep))
    
        print(result)
    3. Others' Solutions
    for in文を使用してに直接アクセスし、[]インデックス演算子を使用して
  • str文字列の各文字にアクセスするのではなく
    import sys
    
    n = int(sys.stdin.readline().strip())
    
    for i in range(n):
        rep, str = sys.stdin.readline().strip().split()
        result = ""
       
        for j in str:
            result += j * int(rep)
    
        print(result)
    4. Learned
  • 文字列の各要素はfor in文を使用して
  • にそれぞれアクセスできます.

    No. 1157


    1. Problem

    2. My Solution
  • 時間長すぎる
  • import sys
    
    word = [char.lower() for char in  sys.stdin.readline().strip()]
    alpha_count = []
    
    for i in range(ord('a'), ord('z')+1):
        alpha_count.append(word.count(chr(i)))
    
    if alpha_count.count(max(alpha_count)) > 1 :
        print("?")
    else:
        print(chr(alpha_count.index(max(alpha_count))+97).upper())
        
    
    3. Others' Solutions
  • メソッド1
    -入力した文字列を直接入力します.upper()関数の適用
  • mport sys
    
    word = sys.stdin.readline().upper()
    alpha_count = []
    
    for i in range(ord('A'), ord('Z')+1):
        alpha_count.append(word.count(chr(i)))
    
    if alpha_count.count(max(alpha_count)) > 1 :
        print("?")
    else:
        print(chr(alpha_count.index(max(alpha_count))+65))
  • メソッド2
    -setデータ型を使用して重複するアルファベットを削除し、リストに再挿入して判断する
    -
  • インデックスを使用して最も多くのアルファベット数を表します.
    import sys
    
    word = sys.stdin.readline().strip().upper()
    char_list = list(set(word))
    alpha_count = []
    
    for char in char_list:
        alpha_count.append(word.count(char))
    
    if alpha_count.count(max(alpha_count)) > 1 :
        print("?")
    else:
        print(char_list[alpha_count.index(max(alpha_count))])
    4. Learned
  • 全文字列直接上下関数
  • SETによる冗長値の除去
  • インデックス
  • を活用

    No. 2908


    1. Problem

    2. My Solution
  • の角数
  • を直接比較した.
    import sys
    
    a,b = sys.stdin.readline().strip().split()
    a,b = a[::-1], b[::-1]
    
    if int(a) > int(b):
        print(a)
    else:
        print(b)
        
    
    3. Others' Solutions
  • 角数リスト、最大関数
  • を使用
    print(max(input()[::-1].split()))
    4. Learned
    文字列[:-1]を使用して
  • 文字列を逆出力します[:-1]
  • 大数または小数を求める場合は、max/minを使用して
  • を試します.

    No. 2908


    1. Problem

    2. My Solution
    import sys
    
    alpha_num = [['A','B','C'],['D','E','F'],['G','H','I'],['J','K','L'],['M','N','O'],['P','Q','R','S'],['T','U','V'],['W','X','Y','Z']]
    
    word = list(sys.stdin.readline().strip())
    total_time = 0
    
    for i in word:
        for j in range(len(alpha_num)):
            if i in alpha_num[j]:
                total_time += j+3
    
    print(total_time)
        
    
    4. Learned
    以下のコードは
  • Vscodeで正しく実行するが、Back Junで実行時エラー
  • が発生する.
  • 行の場合は「」
  • を追加
    import sys
    
    alpha_num = [['A','B','C'],['D','E','F'],['G','H','I'],['J','K','L'],
    ['M','N','O'],['P','Q','R','S'],['T','U','V'],['W','X','Y','Z']]
    
    word = list(sys.stdin.readline().strip())
    total_time = 0
    
    for i in word:
        for j in range(len(alpha_num)):
            if i in alpha_num[j]:
                total_time += j+3
    
    print(total_time)