WUSTOJ 1277: 小吉吉读书(Java)

版权声明:转载请注明出处 https://blog.csdn.net/pfdvnah/article/details/89433279

1277: 小吉吉读书

题目

  有一本 n 页的书,每天都看 ai 页,已知星期 k 买的书,问星期几能看完?更多内容点击标题。

分析

  统计出一个星期能看 a 页,看了 a 页又会回到买书的那一天(k),因此直接用 n 对 a 取余。然后再一天一天看,直到看完为止。

代码

/**
 * time 356ms
 * @author PengHao
 * @version A1.0
 * @date 2019-04-21 下午1:57:17
 */

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n; // 书的页数
		int[] ai = new int[8]; // 每天看的页数
		int startDay; // 买书日
		while (sc.hasNext()) {
			n = sc.nextInt();
			ai[0] = 0; // 初始一周看0页
			for (int i = 1; i < 8; i++) {
				ai[i] = sc.nextInt();
				ai[0] += ai[i];
			}
			startDay = sc.nextInt();
			n %= ai[0]; // 保证一周内看完剩下的页数
			int i = startDay;
			if (0 == n) { // 经过7的整数倍的天数刚好看完
				do {
					i--;
					if (i < 1) {
						i = 7;
					}
				} while (0 == ai[i]);
			} else { // 还有多余的没看完
				while (n > ai[i]) { // 一天一天看,看完为止
					n -= ai[i++];
					if (i > 7) {
						i = 1;
					}
				}
			}
			System.out.println(i);
		}
		sc.close();
	}

}

写在最后:

  1. 如需转载,请于标题下注明链接形式的wowpH的博客即可;
  2. 代码原创,如需公开引用,不能删除首行注释(作者,版本号,时间等信息)。
  3. 如果有疑问欢迎评论留言,尽力解答。

猜你喜欢

转载自blog.csdn.net/pfdvnah/article/details/89433279