网上海投的,感觉他们家的HR好勤快, 联系后第三天就约电面。Hiring Manager 问了我一堆C++的基础问题,还有多线程的东西
然后给我一个coding assignment 让我写好后发给HR
Say you have a deck of cards, and two piles they can be put in.
Initially all cards are in pile A, and pile B is empty.
You are instructed to shuffle the deck as follows:
1. Move top `floor(n/2)` cards in pile A to the bottom of the same pile ("Cut the deck").
2. Pull a card from the top of pile A, and put on pile B
3. Pull a card from the top of pile A, and put it in the bottom of pile A
4. Repeat steps 2 & 3 until pile A is empty.
This counts as one round.
Create a function that takes in the size of the deck as an argument, and prints the
number of rounds needed for the cards to return to their starting position.
我按照这个问题的流程写出来了,也没看出啥规律,所以也不知道到底怎么优化好
卡片数量 n = 1 ->52
It took 2310 rounds to return 52 to their starting position
It took 9 to their starting position
It took 12 rounds to return 8 to their starting position
It took 3 rounds to return 7 to their starting position
It took 5 rounds to return 6 to their starting position
It took 5 rounds to return 5 to their starting position
It took 4 rounds to return 4 to their starting position
It took 3 rounds to return 3 to their starting position
It took 1 rounds to return 2 to their starting position
It took 1 rounds to return 1 to their starting position
唉..但是感觉output好诡异,也不知道怎么证说这是对的...手动尝试了n = 1 ->10的cases,结果是match的..
但是当n=51。。。到n=52跳跃也太大了...
有兴趣的可以看看我写的
|