蜗牛

描述传说中能站在金字塔顶的只有两种动物,一种是鹰,一种是蜗牛。一只小蜗牛听了这个传说后,大受鼓舞,立志要爬上金字塔。为了实现自己的梦想,蜗牛找到了老鹰,老鹰告诉它金字塔高H米,小蜗牛知道一个白天自己能向上爬10米,但由于晚上要休息,自己会下滑5米。它想知道自己在第几天能站在金字塔顶,它想让你帮他写个程序帮助它。
输入一个整数H(0<H<10^9)代表金字塔的高度。
输出一个整数n表示小蜗牛第n天站在金字塔顶上

public class Demo1 {
    public static void main(String[] args) {
        System.out.println("请输入高度:"); //增强用户体验
        Scanner scanner=new Scanner(System.in);
        int height=scanner.nextInt();
        int days=m1(height);
        System.out.println("需要的天数:"+days);
    }
    
    public static int m1(int height) {
        //8848
        int sum=0;
        int count=0; //保存需要的天数
        while(sum<height){
            count++; //每一次循环,就加一天
            sum+=10;
            if(sum>=height){ //加10天的时候已经站在了顶点
                break;
            }
            sum-=5;
        }
        return count;
    }
}
原文地址:https://www.cnblogs.com/Heng23/p/8001154.html