题解
2025-08-17 19:25:12
发布于:浙江
8阅读
0回复
0点赞
90分暴力模拟代码
#include<iostream>
#include<cstdio>
typedef long long ll;
using namespace std;
const int N = 1e7+10;
int n;
bool flag[N] = {0};
struct apple{
int present_id,begin_id;
bool flag;
int day;
}a[N];
int main(){
freopen("apple.in","r",stdin);
freopen("apple.out","w",stdout);
int days = 0;
cin >> n;
for(int i = 1;i <= n;i++){
a[i].begin_id = i;
a[i].flag = false;
}
int ans = 0;
while(true){
if(ans == n) break;
days++;//天数
for(int i = 1;i <= n;i++){
if(a[i].flag == false){//寻找当前首个未被拿走的苹果
//cout << a[i].begin_id << " ";
ans++;//总拿走苹果数+1
a[i].day = days;//标记拿走苹果的时间
a[i].flag = true;//拿走找到的第一个苹果
int cnt = 0;
for(int j = a[i].begin_id;j <= n;j++){
if(cnt == 2 && a[j].flag == false){//找到当前与1号苹果间隔为2且未被拿走的苹果
//cout << a[j].begin_id << " ";
a[j].flag = true;//拿走苹果
a[j].day = days;//标记苹果被拿走的时间
ans++;//总拿走苹果数+1
cnt = 0;//重新计数
a[j].present_id = 1;//更新1号苹果
}else if(cnt != 2 && a[j].flag == false){
cnt++;//跳过当前未被标记但与1号苹果间隔不是2的苹果,计数
}else{//跳过已被拿走的苹果
continue;
}
}
break;
}
}
}
cout << days << " " << a[n].day;
fclose(stdin);
fclose(stdout);
return 0;
}
满分代码
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n;
int main(){
freopen("apple.in","r",stdin);
freopen("apple.out","w",stdout);
cin >> n;
ll sum = 0,ans = 0;
while(n--){
sum++;
if(ans == 0 && n % 3 == 0) ans = sum;
n = n - ceil(n/3);//每次减少1/3,注意向上取整
}
cout << sum << ' ' << ans;
fclose(stdin);
fclose(stdout);
return 0;
}
点个赞吧
全部评论 1
权威
2025-08-17 来自 浙江
0
有帮助,赞一个