Description
We are given a integer sequence, your job is find the length of the longest contiguous subsequence that is strictly increasing or strictly decreasing.
Input
- First number?, represent how many test cases.
- For each test case the first number is?.
- Then??positive integers are followed, all of them are less than 101.
Output
For each test case output the answer in one line.
Sample Input
3
3 1 1 1
3 1 2 3
4 4 3 2 1
Sample Output
1 3 4
思路: 用2個數組存取遞增遞減序列,并且浪費點空間節省效率的方法去做。
1 #include<stdio.h> 2 int main() 3 { 4 int i,j,test,num,len,A[ 50 ],B[ 50 ],C[ 50 ]; 5 scanf( " %d " ,& test); 6 for (i= 0 ;i<test;i++ ) 7 { 8 scanf( " %d " ,& num); 9 for (j= 0 ;j<num;j++ ) 10 scanf( " %d " ,& C[j]); 11 A[ 0 ]= 1 ; 12 B[ 0 ]= 1 ; 13 for (j= 1 ;j<num;j++ ) 14 { 15 if (C[j]>C[j- 1 ]) 16 { 17 A[j]=A[j- 1 ]+ 1 ; 18 B[j]= 1 ; 19 } 20 if (C[j]<C[j- 1 ]) 21 { 22 A[j]= 1 ; 23 B[j]=B[j- 1 ]+ 1 ; 24 } 25 if (C[j]==C[j- 1 ]){ 26 A[j]= 1 ; 27 B[j]= 1 ; 28 } 29 } 30 for (j= 0 ,len= 0 ;j<num;j++ ) 31 { 32 if (A[j]>len) len= A[j]; 33 if (B[j]>len) len= B[j]; 34 } 35 printf( " %d\n " ,len); 36 } 37 return 0 ; 38 }
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
