-->
当前位置:首页 > 题库

PROGRAMMING:Sum of two numbers

Luz5年前 (2021-05-10)题库377
Given a group of integers and an objective number, find two numbers in the given group of integers and make the sum of them the objective number. If found, the solution is unique. If not found, "no answer" will be displayed. The subscripts of the output are sorted from small to large. It is realized by adding a dictionary to a single loop.
###Input format:
Give this set of numbers in a row.
Enter the target number on the next line
###Output format:
Output the subscripts of the two numbers in one line, separated by a space.
###Input sample 1:
Here is a set of inputs. For example:
```in
2,7,11,15
nine
```
###Output sample 1:
The corresponding output is given here. For example:
```out
0 1
```
###Input sample 2:
Here is a set of inputs. For example:
```in
3,6,9
ten
```
###Output sample 2:
The corresponding output is given here. For example:
```out
no answer
```







answer:If there is no answer, please comment
```
nums = list(map(int,input().split(',')))
target = int(input())
hm = dict()
for i in range(len(nums)):
if nums[i] in hm:
print(hm[nums[i]], i)
break
hm[target - nums[i]] = i
else:
print("no answer")
```