程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Blue Bridge Cup basic exercise finding integer Python

編輯:Python

Resource constraints

The time limit :1.0s Memory limit :256.0MB

Problem description

Give a containing n A sequence of integers , Ask for integers a The first occurrence in a sequence is the number of .

Input format

The first line contains an integer n.

The second line contains n Nonnegative integers , For a given sequence , Each number in the sequence is not greater than 10000.

The third line contains an integer a, For the number to be found .

Output format

If a In the sequence , Output where it first appeared ( Location slave 1 Numbered starting ), Otherwise output -1.

The sample input

6
1 9 4 8 3 9
9

Sample output

2

Implementation code

n=int(input())
list = list(map(int,input().split()))
m =int(input())
try:
print(list.index(m)+1)
except:
print(-1)

matters needing attention

map The usage function

map yes python Built in functions , The specified sequence will be mapped according to the provided function .
stay python3 in map To match list Use if not list

n=int(input())
list = map(int,input().split())
m =int(input())
print(list)
try:
print(list.index(m)+1)
except:
print(-1)

Output results

6
1 9 4 8 3 9
9
<map object at 0x000002543716FFA0>
-1

reason
python3 in map() return iterators type , No more python2 Medium list type . We need to use it ourselves list transformation .
iterators The type is python Iterators in .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved