UVA413 LA5388 POJ1492 ZOJ1338 Up and Down Sequences【序列处理】

509 篇文章 9 订阅
281 篇文章 4 订阅

The quality of pseudo random-number generators used in some computations, especially simulation, is a significant issue. Proposed generation algorithms are subjected to many tests to establish their quality, or, more usually, their lack of it. One of the common tests is the run test.
    In this test, sequences are tested for “runs up” and “runs down.”
    We will examine series of data values for the “Up” and “Down” sequences each series contains.
    Within a series, an “Up” sequence continues as long as each data-value received is not less than the previous data-value. An “Up” sequence terminates when a data-value received is less than the previous data-value received.
    A “Down” sequence continues as long as each data-value received is not greater than the previous data-value. A “Down” sequence terminates when a data-value received is greater than the previous data-value received.
    An “Up” sequence can be initiated by the termination of a “Down” sequence and vice versa. (Sequences initiated in this manner have length one at this initiation point.)
    All the initial data-values are part of an “Up” sequence, and contribute to its length, if the first deviation of the data-values is upwards.
    All the initial data-values are part of a “Down” sequence, and contribute to its length, if the first deviation of the data-values is downwards.
    If the data-values received don’t allow classification as either an “Up” or a “Down” sequence, the data should be considered to have neither sequence.
    Find the average length of both the “Up” and the “Down” sequences encountered for each input line in the data file. Report these average lengths as each input line is processed.
Input
Each of the separate series to be examined is contained on a single line of input.
    Each series to be analyzed consists of at least one and no more than 30 unsigned, non-zero integers. Each integer in a series has at least one digit and no more than four digits. The integers are separated from each other by a single blank character. Each of the series will be terminated by a single zero (0) digit. This terminator should not be considered as being part of the series being analyzed.
    The set of series to be analyzed is terminated by a single zero (0) digit as the input on a line. This terminator should not be considered to be a series, and no output should be produced in response to its encounter.
Output
A line with two real values is to be emitted for each input data set encountered. It must begin with the message “Nr values = N: ”, where N is the number of input data in the line; and then to continue with the average values for runs.
    First, the average “Up” run length, then the average “Down” run length. Separate these values with a space.
    Answers must be rounded to six digits after the decimal point.
Note: Your output for the float/real, using your chosen language, may be default-formatted differently.
Sample Input
1 2 3 0
3 2 1 0
1 2 3 2 1 0
2 2 2 2 3 0
4 4 4 4 3 0
4 4 4 3 3 3 3 0
4 4 4 3 3 3 4 0
5 5 5 5 0
1 2 3 2 3 4 5 0
0
Sample Output
Nr values = 3: 2.000000 0.000000
Nr values = 3: 0.000000 2.000000
Nr values = 5: 2.000000 2.000000
Nr values = 5: 4.000000 0.000000
Nr values = 5: 0.000000 4.000000
Nr values = 7: 0.000000 6.000000
Nr values = 7: 1.000000 5.000000
Nr values = 4: 0.000000 0.000000
Nr values = 7: 2.500000 1.000000

Regionals 1995 >> North America - East Central NA

问题链接UVA413 LA5388 POJ1492 ZOJ1338 Up and Down Sequences
问题简述:(略)
问题分析
    繁琐的序列处理和数学计算问题,不解释。
    实际上就是一个上升与下降的统计计算问题。
程序说明
    POJ中需要用格式"%.6f",似乎用"%.6lf"会WA,这是一个大坑!!!
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA413 LA5388 POJ1492 ZOJ1338 Up and Down Sequences */

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

int eqcnt;     // 相邻相等计数
int lcnt;     // 相邻变为小于计数
int gcnt;   // 相邻变为大于计数
int all_lecnt;       // 相邻所有的小于等于计数(存在小于,只有相等不计数)
int all_gecnt;      // 相邻所有的大于等于计数(存在大于,只有相等不计数)
int lecnt;        // 相邻小于等于计数
int gecnt;      // 相邻大于等于计数
int lflag;       // 相邻出现小于标记
int gflag;      // 相邻出现大于标记

int main()
{
    int a;
    while(~scanf("%d", &a) && a) {
        int cnt = 1;

        vector<int> v;
        v.push_back(a);
        while(~scanf("%d", &a) && a) {
            v.push_back(a);
            cnt++;
        }

        eqcnt = lcnt = gcnt = all_lecnt = all_gecnt = lecnt = gecnt = lflag = gflag = 0;
        for(int i = 1; i < cnt; i++)
            if(v[i - 1] < v[i]) {
                if(lflag == 0)
                    lflag = 1, gflag = 0, lcnt++;
                lecnt++, lecnt += eqcnt;
                all_lecnt++, all_lecnt += eqcnt;
                eqcnt = 0;
                gecnt = 0;
            } else if(v[i - 1] > v[i]) {
                if(gflag == 0)
                    gflag = 1, lflag = 0, gcnt++;
                gecnt++, gecnt += eqcnt;
                all_gecnt++, all_gecnt += eqcnt;
                eqcnt = 0;
                lecnt = 0;
            } else {        // v[i - 1] == v[i]
                if(lecnt > 0) all_lecnt++;
                else if(gecnt > 0) all_gecnt++;
                else eqcnt++;
            }
        if(lcnt == 0) lcnt = 1;
        if(gcnt == 0) gcnt = 1;

        printf("Nr values = %d:  %.6f %.6f\n", cnt, (double)all_lecnt / lcnt, (double)all_gecnt / gcnt);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值