当前位置: 代码迷 >> 综合 >> HDOJ1555 How many days?
  详细解决方案

HDOJ1555 How many days?

热度:72   发布时间:2023-12-14 01:40:22.0

How many days?

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8506    Accepted Submission(s): 4922


Problem Description
8600的手机每天消费1元,每消费K元就可以获赠1元,一开始8600有M元,问最多可以用多少天?

Input
输入包括多个测试实例.每个测试实例包括2个整数M, k,(2 <= k <= M <= 1000).M = 0, k = 0代表输入结束.

Output
对于每个测试实例输出一个整数,表示M元可以用的天数。

Sample Input
   
2 2 4 3 0 0

Sample Output
   
3 5
简单 的模拟题,解释都在代码了
import java.util.Scanner;public class Main{private static Scanner scanner;public static void main(String[] args) {scanner = new Scanner(System.in);while (scanner.hasNext()) {int m = scanner.nextInt();int k = scanner.nextInt();if (m == 0 && k == 0) {break;}int day = 0;while (true) {for (int i = 0; i < k; i++) {// 每次减k元,每减一元如果day>=0就day++// 否则就是负数了,退出form--;if (m >= 0) {day++;} else {break;}}// 到这里如果大于等于0,证明消费满了k次,day应该++// 否则就应该退出if (m >= 0) {m++;} else {break;}}System.out.println(day);}}
}