用整数向上除法统计被吃完的苹果数量并从总数中扣除。
OJ: noi_openjudge
题目 ID: ch0103-15
难度:入门
标签:数学python
日期: 2026-07-30 23:01
题意
每
思路
只要正在吃某个苹果,它就不再完整,因此被影响的苹果数为 (y + x - 1) // x,从 n 中减去它即可。
代码
Python代码
python
apples, hours_per_apple, hours = map(int, input().split())
eaten = (hours + hours_per_apple - 1) // hours_per_apple
print(apples - eaten)C++代码
cpp
#include <cstdio>
int main(){
int n,x,y;
scanf("%d%d%d",&n,&x,&y);
int ans = y / x;
if( y % x != 0)
ans++;
printf("%d",n-ans);
return 0;
}复杂度
时间复杂度和额外空间复杂度均为
总结
“剩余完整物品”要把已开始但未完成的操作也计入损失,因此使用向上取整。