Consecutive numbers for
Question number: | 201409-1 |
Questions Name: | Consecutive numbers for |
time limit: | 1.0s |
Memory Limit: | 256.0MB |
Problem Description: |
Problem Description
N different integers to be, ask how much of an integer number of these have their values differ by exactly 1.
Input Format
Comprising a first line of input integer n, given the number represents integers.
The second line comprises a given n integers.
Output Format
Output an integer value representing a difference of just a few number of pairs.
Sample input
6
10 2 6 3 7 8
Sample Output
3
Sample Description
Difference between the number of a value of exactly 1 comprises a pair (2, 3), (6, 7), (7, 8).
Evaluation scale cases and agreed with
1 <= n <= 1000, given an integer of not more than 10,000 non-negative integer.
|
Analysis: This problem is relatively simple (refer to negation ), +1 is determined whether there traversal, the final output can (where there is a pit, is the logarithm of the requirements of the subject, so there is no duplication, the finished brush title In contrast the number of easy to fall into the pit do when this question).
Code:
n=int(input())
num = input().split()
#用于记录数的个数
a=0
for i in range(len(num)):
#因为num是str类型的,所以先转成int,再转成str
if str(int(num[i])+1) in num:
a+=1
#不需要处理
print(a)
result:
No submit | Questions Name | Programming language | Evaluation results | Time use | Use of space |
---|---|---|---|---|---|
1794570 | Consecutive numbers for | python | correct | 46ms | 8.679MB |