2023 Huawei OD Computer-Based Unified Examination Paper B [Find the Minimum Number of Steps] C Language Implementation [100% Pass Rate]

Table of contents

topic

Ideas

Code

Require


topic

Find the minimum number of steps from coordinate zero point to coordinate point n. You can only move 2 or 3 to the left or right along the abscissa axis at a time. Note: The coordinate points of the path can be negative input description. Coordinate
point
n
output description
output from coordinate zero point Minimum number of steps to move to coordinate point n
Remarks
1 <= n <= 10^9

Example 1:
Input

4
outputs

2Explanation
:
Moving from coordinate zero to 4 requires at least two steps, that is, moving right by 2, and then moving right by 2.

Ideas

This question is a bit like a reasoning question, looking for patterns.

If there is 2 in the minimum number of steps of a number, then n+1 actually changes 2 to 3, and the minimum number of steps remains unchanged. For example
: 4 =2+2 and 5 =3+

Guess you like

Origin blog.csdn.net/misayaaaaa/article/details/132746968