pat basic 1112 exceedance range

The above picture is a line chart drawn using data collected in a certain scientific research. The red horizontal line represents the threshold of normal data (the threshold in this picture is 25). Your task is to find the interval where the abnormal data exceeds the threshold. For example, in the figure above, three data points in the interval [3, 5] on the horizontal axis exceed the standard, and the data point corresponding to point 9 on the horizontal axis (can be expressed as the interval [9, 9]) also exceeds the standard.

Input format:

The first line of input gives two positive integers N (≤104) and T (≤100), which are the number of data points and the threshold respectively. The second line gives the ordinates of N data points, all of which are positive integers not exceeding 1000, and the corresponding abscissas are integers 0 to N−1.

Output format:

Output the intervals of excessive data in order from left to right, each interval occupies one line, and the format is [A, B], where A and B are the left and right endpoints of the interval. If no data exceeds the standard, the maximum value of all data is output in one row.

Input example 1:

11 25
21 15 25 28 35 27 20 24 18 32 23

Output sample 1:

[3, 5]
[9, 9]

Input example 2:

11 40
21 15 25 28 35 27 20 24 18 32 23

Output sample 2:

35

Problem-solving ideas:

Pay attention to consider the situation where the data at the end exceeds the standard

#include <stdio.h>

int main(int argc, const char *argv[]) {
    int N, T, i, begin = -1, end, data, max = 0;

    if ( scanf("%d %d", &N, &T)==EOF ) printf("error\n");
    for ( i=0; i<N; ++i ) {
        if ( scanf("%d", &data)==EOF ) printf("error\n");
        if ( data > max ) max = data;
        if ( begin < 0 && data > T ) {
            begin = i;
            if ( i == N-1 ) {
                end = i;
                printf("[%d, %d]\n", begin, end);
            }
        } else if ( begin >= 0 && data <= T ) {
            end = i - 1;
            printf("[%d, %d]\n", begin, end);
            begin = -1;
        } else if ( begin >= 0 && data > T && i == N-1 ) {
            end = i;
            printf("[%d, %d]\n", begin, end);
        }
    }
    if ( max <= T ) printf("%d\n", max);

    return 0;
}

Guess you like

Origin blog.csdn.net/herbertyellow/article/details/129090540
Recommended