题目大意:你现在站在数轴的坐标原点处,要前往x=n处(n在原点右侧),你每次行动可以向右走3格/走两格或者向左走三格/走两格,可以走到原点左侧,问最少需要多少次行动。
思路:我们在草稿纸上模拟一下可以发现,只有当n=1时我们不得不先向右走再向左走,其余情况都可以先向右3格3格的字,直到剩余2格或4格,所以都是n/3向上取整
- #include
- using namespace std;
- int main()
- {
- int t;
- cin >> t;
- while (t--)
- {
- int n;
- cin >> n;
- if (n == 1)//特判n=1的情况
- {
- printf("2\n");
- continue;
- }
- printf("%d\n", (int)ceil(n*1.0 / 3));
- }
- return 0;
- }