您的位置:首页 > 产品设计 > UI/UE

ZOJ 1338 Up and Down Sequences

2012-08-24 10:37 399 查看
Up and Down Sequences

Time Limit: 2 Seconds Memory Limit: 65536 KB

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.

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

题意:就是找升序序列的平均长度和降序序列的平均长度。
注意:像先升序,再降,再升例如这样的情况,因为一串里面有2个升序序列,所以要将升序序列的长度/2才是平均长度
//注意设置状态变量是关键,EVEN这个状态只有初始才会出现,所以最后个else不用变状态,
//因为后面出现的EVEN要么属于UP,要么属于DOWN

//所以不用改变状态量

代码:
#include <stdio.h>

const int UP=1,DOWN=-1,EVEN=0;

int main()

{

int n;

while(scanf("%d",&n))

{

if(n==0)

break;

int pre=n,cur,cnt=1,sta=EVEN;

int lenup=0,lendow=0,lene=0,numup=0,numdown=0;

while(scanf("%d",&cur))

{

if(cur==0)

break;

cnt++;

if(cur>pre)

{

if(sta==UP)

lenup++;

else if(sta==DOWN)

{

numdown++;

lenup++;

sta=UP;

}

else{

lenup+=lene+1;

sta=UP;

}

}

else if(cur<pre)

{

if(sta==DOWN)

lendow++;

else if(sta==UP)

{

numup++;

lendow++;

sta=DOWN;

}

else

{

lendow+=lene+1;

sta=DOWN;

}

}

else{

if(sta==UP)

lenup++;

else if(sta==DOWN)

lendow++;

else

lene++;

}

pre=cur;

}

if(sta==UP)

numup++;

else if(sta==DOWN)

numdown++;

double upave=0,downave=0;

if(numup!=0)

upave=1.0*lenup/numup;

if(numdown!=0)

downave=1.0*lendow/numdown;

printf("Nr values = %d: %.6lf %.6lf\n",cnt,upave,downave);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: