HackerRank:Hash Tables: Ice Cream Parlor Python3

题目

https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=search

题目解析

使用字典,找到相关的值即可,但是要注意要把自己排除掉,可能出现自己加自己相等的情况

ANSWER

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the whatFlavors function below.
def whatFlavors(cost, money):
    #cost_m={v:k for k,v in enumerate(cost)}
    cost_m={}
    for k,v in enumerate(cost):
        if money-v in cost_m.keys():
            print(cost_m[money-v]+1,k+1)
        cost_m[v]=k


if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        money = int(input())

        n = int(input())

        cost = list(map(int, input().rstrip().split()))

        whatFlavors(cost, money)

你可能感兴趣的:(HackerRank:Hash Tables: Ice Cream Parlor Python3)