题解:
分析数据:由于数据n已经大于107,(考试用的评测计算机是1秒运行107次)所以就不可能是O(n)的复杂度。
通过打草稿我们发现:
如果第n个苹果在当天被取走,当天的苹果数量一定是3的倍数+1个。
最后一个苹果在当天没有被取走之前,在下一天肯定是最后一个。
Std:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int ans = 0, cnt = 0;//ans表示第几天取走第n个苹果,cnt表示取走所有苹果的天数
bool flag = 0; //第n个苹果有没有被取走?
while (n)
{
cnt++;
if (flag == 0 && n % 3 == 1)
{
flag = 1;
ans = cnt;
}
n -= ceil(n / 3.0); //剩下的苹果 ceil(x)上取整函数
}
cout << cnt << " " << ans << endl;
return 0;
}
时间复杂度:O(Log3n)这样109的数据最多也就是运行50次。