洛谷Java入门代码之分苹果

题目描述

八尾勇喜欢吃苹果。她现在有 m(m≤100)m(m\le 100)m(m≤100) 个苹果,吃完一个苹果需要花费 t(t≤100)t(t \le100)t(t≤100) 分钟,吃完一个后立刻开始吃下一个。现在时间过去了 s(s≤10000)s(s\le 10000)s(s≤10000) 分钟,请问她还有几个完整的苹果?
输入格式

输入三个非负整数表示 m 、t 和 s。
输出格式

输出一个整数表示答案。
输入输出样例
输入 #1

50 10 200

输出 #1

30
import java.util.*;
public class P5709 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int t = sc.nextInt();
        int s = sc.nextInt();
        sc.close();
        int sum = (int)s/t;
        if (t==0||m==0||m*t<s){   
        //吃一个的时间为0,一瞬间吃完;苹果数量为0,直接输出0;
        //苹果的数量与吃每个的积小于过去的时间数,表明已经吃完,为0个
            System.out.print("0");
        }
         else if(s==0){
        //过去0分钟,直接剩下总数m
            System.out.print(m);
        }
        else if (s%t==0){
        //s模t,表明吃掉整数个,剩余完整个数=总数-吃掉的个数
            System.out.print(m-sum);
        }
        else {
        //其他时间段内,吃掉一点的都不算完整,所以要-1
            System.out.print(m-sum-1);
        }
    }
}
发布了31 篇原创文章 · 获赞 1 · 访问量 189

猜你喜欢

转载自blog.csdn.net/weixin_44048403/article/details/105331523