这道题有够无聊……
看了半天都没看懂题目是什么,于是直接找了一个能过的代码
后来发现原来就是找连续的上升,下降的序列的长度的平均数
1 #include <stdio.h>
2
3 int upCount, downCount, pendingCount;
4 int numberOfUpSequences, numberOfDownSequences;
5
6 char state;
7
8 int thisValue, lastValue;
9
10 int main()
11 {
12 float avgUp, avgDown;
13 int done = 0;
14 int count;
15 while ( ! done )
16 {
17 state = 'S';
18 upCount = 0; downCount = 0; pendingCount = 0;
19 count = 0;
20 numberOfUpSequences = 0; numberOfDownSequences = 0;
21 while ( 1 )
22 {
23 scanf("%d", & thisValue);
24 if ( (state == 'S') && (thisValue == 0 ) )
25 {
26 done = 1;
27 break;
28 }
29 else if (thisValue == 0)
30 break;
31 switch ( state )
32 {
33 case 'S':
34 state = 'N';
35 break;
36 case 'N':
37 if ( thisValue == lastValue )
38 pendingCount++;
39 else if ( thisValue < lastValue )
40 {
41 state = 'D';
42 downCount = pendingCount + 1;
43 numberOfDownSequences++;
44 }
45 else if ( thisValue > lastValue )
46 {
47 state = 'U';
48 upCount = pendingCount + 1;
49 numberOfUpSequences++;
50 }
51 break;
52 case 'D':
53 if ( thisValue <= lastValue )
54 downCount++;
55 else if ( thisValue > lastValue )
56 {
57 state = 'U';
58 upCount++;
59 numberOfUpSequences++;
60 }
61 break;
62 case 'U':
63 if( thisValue >= lastValue )
64 upCount++;
65 else if ( thisValue < lastValue )
66 {
67 state = 'D';
68 downCount++;
69 numberOfDownSequences++;
70 }
71 break;
72 }
73 lastValue = thisValue;
74 count++;
75 }
76 if ( state != 'S' )
77 {
78 if ( numberOfUpSequences )
79 avgUp = (float)upCount/numberOfUpSequences;
80 else
81 avgUp = 0.0;
82 if ( numberOfDownSequences )
83 avgDown = (float)downCount/numberOfDownSequences;
84 else
85 avgDown = 0.0;
86 printf("Nr values = %d: ", count);
87 printf("%f %f\n", avgUp, avgDown );
88 }
89 }
90 }
91
92