알고리즘

[BAEKJOON] 4287 Word Ratios

mAlfred 2025. 6. 5. 18:35
반응형

문제

Many ‘IQ’ tests have questions of the form: king : queen :: president : ?, where the ‘correct’ answer (in USA anyway) is ‘first lady’, which says a lot about IQ tests and western culture. Because these tests are so culture laden, psychologists at the University of Northern Southwestland have devised a similar test, based on the positional difference between the letters in the words. Thus a typical problem might be: cat : dog :: emu : ? to which the answer is ‘fah’ because to go from ‘cat’ to ‘dog’ you advance the first letter by 1, the second by 14, and the third by 13. So ‘cat’ to ‘dog’ = ‘emu’ to ‘fah’. However, these same psychologists are somewhat arithmetically challenged, so they are never quite sure that they have got the right answer. This is where you come in.

Write a program that will read in triples of words such as the ones above and determine the fourth word according to the rules outlined. Consider that the lower case alphabet wraps around at both ends, i.e. ‘a’ succeeds ‘z’ and ‘z’ precedes ‘a’.

입력

Input will consist of a series of problems. Each problem will consist of three words on a line (see the definition of a word in the Preamble), separated by single spaces. All the words on a line will be the same length (not more than 20 letters), but words on different lines may be of different lengths. Input will be terminated by a line consisting of a single ‘#’ character.

출력

Output will consist of one line for each line of input consisting of the three words given in the input followed by the ‘answer’, all separated by single spaces. Note that the answer must also be a word, i.e. it must be of the same length as the three input words and consist only of lower case letters.

예제 입력 1 복사

cat dog emu
frog wolf bear
#

예제 출력 1 복사

cat dog emu fah
frog wolf bear sbxq

IQ 문제라고 한다.

3개의 단어가 나왓을 때 

그 해당 문자 3번째에 다음에 나올 문자를 알아내라 라는 문제임

그냥 이건 첫번째 단어들과 두번째 단어들의 차를 구하고 3번째 단어를 그 차랑 구하면 되는 문제

 

alpha = [chr(i) for i in range(ord('a'), ord('z')+1)]

while True:
    q1 = []
    a = input()
    if a == '#':
        break
    
    wd = a.split()

    for i in range(len(wd[0])):
        q = wd[0][i]
        w = wd[1][i]
        e = wd[2][i]
        dif  = ord(w) - ord(q) 
        dif2 = ord(q) - ord(w)

        q1.append(alpha[(alpha.index(wd[2][i]) + dif) % len(alpha)])
    
    print(*wd, '', end = '')
    for s in q1:
        print(s, end = '')
    print()
반응형