Visa OA 面试真题解析:Counting Game(数字报数变体)

17次阅读
没有评论

Counting Game

The counting game is a widely popular casual game. Every participant in the game must count numbers in sequence. However, if the next number to be called is a multiple of 7, or if the next number contains the digit 7, then that number must be skipped; otherwise, you lose the game.

Rose and Zack play this game and find it too easy, so they decide to modify some rules: for any number containing the digit 7, all its multiples cannot be called either.

For example, if Rose calls out 6, since 7 cannot be called, Zack must call 8 next. If Rose calls out 33, since 34 is 17 times 2 and 35 is 7 times 5, Zack’s next possible call is 36. If Rose calls out 69, because numbers 70 to 79 all contain the digit 7, Zack’s next call must be 80.

Input Format

Input a line which contains a positive integer X, indicating the number called by Rose this time.

Output Format

Output a line which contains an integer.

If the number called by Rose this time is invalid (cannot be called), output -1. Otherwise, output the number that Zack should call next.

Sample Input 1

6

Sample Output 1

8

Sample Input 2

33

Sample Output 2

36

这题本质上是“找下一个合法数字”。规则是:某个数字如果本身含有 7,或者是含有 7 的数字的倍数,那么它都不能被报出。解题时先判断输入 X 是否合法;如果 X 不合法,直接输出 -1。否则从 X+1 开始不断向后尝试,直到找到第一个满足“既不含 7,也不是任何含 7 数字的倍数”的数并输出即可。由于题目只要求找到下一个可报数,直接顺序检查就能通过,关键在于正确判断一个数是否包含 7,以及它是否会被某个含 7 的数整除。

正文完
 0