P1085 不高兴的津津

文章目录

题目信息

字段 内容
题号 P1085
难度 入门(NOIP 2004 普及组)
知识点 模拟
来源 NOIP 2004 普及组

题目描述

津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班,另外每周妈妈还会送她去学习朗诵、舞蹈和钢琴。

但是津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。假设津津不会因为其它事不高兴,并且她的不高兴不会持续到第二天。

请你帮忙检查一下津津下周的日程安排,看看下周她会不会不高兴;如果会的话,哪天最不高兴。

输入格式

输入共七行,每行两个整数 a 和 b,分别表示津津当天的上学时间和课外班时间(a, b < 10)。

输出格式

如果津津下周没有不高兴的日子,输出 0;否则输出最不高兴那天的编号(1 表示周一,7 表示周日)。

样例

输入

5 3
6 2
7 2
5 5
9 3
7 5
5 6

输出

4

解题思路

核心思想

这是一道模拟题,只需要按照题意逐天处理即可。

每天有两个输入:上学时间 a 和课外班时间 b。两者相加得到当天总时间 a + b

判断条件:

  • 总时间 > 8 小时:津津不高兴
  • 总时间 > 当前最大值:maxn,则更新 maxd 为当天编号

注意:题目要求上得越久越不高兴,所以要找的是总时间最大且超过 8 的那天,不是第一个超过 8 的天。

完整代码

C++

#include <iostream>

using namespace std;

int study[8];
int extra[8];
int max_duration = 0;
int max_day = 0;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    for (int day = 1; day <= 7; ++day) {
        cin >> study[day] >> extra[day];
        int total = study[day] + extra[day];
        // 仅当总时长超过 8 小时时才考虑更新答案
        // 且只有比当前最大值更大时才更新(保证最不高兴的那天)
        if (total > max_duration && total > 8) {
            max_duration = total;
            max_day = day;
        }
    }

    if (max_duration == 0) {
        cout << 0 << "\n";
    } else {
        cout << max_day << "\n";
    }

    return 0;
}

关键代码讲解

逐天处理

for (int day = 1; day <= 7; ++day) {
    cin >> study[day] >> extra[day];
    int total = study[day] + extra[day];
    if (total > max_duration && total > 8) {
        max_duration = total;
        max_day = day;
    }
}

循环枚举周一到周日的七天,每天读入两个整数,求和后存入 total

关键在于两个条件都要满足

  1. total > max_duration:比当前记录的最大值更大,这样才能找到"最"不高兴的天
  2. total > 8:当天总时长必须超过 8 小时,津津才会不高兴

两者同时满足时才更新答案。如果循环结束后 max_duration 仍为 0,说明没有任何一天超过 8 小时,输出 0

输出答案

if (max_duration == 0) {
    cout << 0 << "\n";
} else {
    cout << max_day << "\n";
}

如果 max_duration 仍为初始值 0,说明七天中没有一天超过 8 小时,输出 0;否则输出记录下的 max_day

复杂度分析

方法 时间复杂度 空间复杂度
模拟 O(7) = O(1) O(1)

由于只有 7 天,算法是常数级别的。

易错点

  1. 找的是"最"不高兴,不是第一个不高兴:需要用 > 而不是 >= 来更新最大值,确保找到的是总时长最长的那天。例如:周一 9+0=9,周三 8+1=9,两个都超过 8,但应该输出周一,因为两天时长相同,周一先出现,而代码中 > 确保只有更大的才更新,相同则不更新,保留先出现的那个。
  2. 不满足 > 8 的天不能更新答案total > max_duration && total > 8 中两个条件缺一不可。如果只判断 total > max_duration,那么初始值 max_duration = 0 时,第一天即使只有 3 小时也会被当作"最大",导致误输出。
  3. 输出的是天数编号,不是时长:答案应该是 1~7 或 0,不要误输出 max_duration

其他语言解法

Python

import sys


def solve() -> None:
    data = sys.stdin.read().strip().split()
    if not data:
        return
    it = iter(data)
    max_duration = 0
    max_day = 0

    for day in range(1, 8):
        a = int(next(it))
        b = int(next(it))
        total = a + b
        if total > max_duration and total > 8:
            max_duration = total
            max_day = day

    print(max_day)


if __name__ == "__main__":
    solve()

Java

import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner(System.in);
        int maxDuration = 0;
        int maxDay = 0;

        for (int day = 1; day <= 7; ++day) {
            int a = fs.nextInt();
            int b = fs.nextInt();
            int total = a + b;
            if (total > maxDuration && total > 8) {
                maxDuration = total;
                maxDay = day;
            }
        }

        System.out.println(maxDay);
    }

    // FastScanner: BufferedInputStream + 手动字节解析,避免 Scanner MLE
    private static class FastScanner {
        private final byte[] buffer = new byte[1 << 16];
        private int ptr = 0;
        private final InputStream in;

        FastScanner(InputStream in) {
            this.in = in;
        }

        private int readByte() throws IOException {
            if (ptr >= buffer.length) {
                int n = in.read(buffer);
                ptr = 0;
                if (n <= 0) return -1;
            }
            return buffer[ptr++];
        }

        int nextInt() throws IOException {
            int c, sign = 1, x = 0;
            do {
                c = readByte();
            } while (c <= ' ' && c != -1);
            if (c == '-') {
                sign = -1;
                c = readByte();
            }
            while (c > ' ') {
                x = x * 10 + (c - '0');
                c = readByte();
            }
            return x * sign;
        }
    }
}

Go

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    maxDuration := 0
    maxDay := 0

    for day := 1; day <= 7; day++ {
        var a, b int
        fmt.Fscan(reader, &a, &b)
        total := a + b
        if total > maxDuration && total > 8 {
            maxDuration = total
            maxDay = day
        }
    }

    if maxDay == 0 {
        fmt.Println(0)
    } else {
        fmt.Println(maxDay)
    }
}

JavaScript

'use strict';

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const lines = [];
rl.on('line', (line) => {
    lines.push(line.trim());
}).on('close', () => {
    solve(lines);
});

function solve(lines) {
    let maxDuration = 0;
    let maxDay = 0;

    for (let day = 1; day <= 7; ++day) {
        const [a, b] = lines[day - 1].split(/\s+/).map(Number);
        const total = a + b;
        if (total > maxDuration && total > 8) {
            maxDuration = total;
            maxDay = day;
        }
    }

    process.stdout.write(String(maxDay));
}

参考链接