首页 > 代码库 > poj 1492 Up and Down Sequences 模拟计数

poj 1492 Up and Down Sequences 模拟计数

水题,直接贴代码。

//poj 1492 
//sep9
#include <iostream>
using namespace std;
int a[64];
int n;
void deal()
{
	int up=0,down=0,upSum=0,downSum=0;	
	int i,j;
	for(i=1;i<n;){
		if(a[i]<a[i+1]){
			++up;
			for(j=i+1;j<=n;++j)
				if(a[j-1]<=a[j])
					++upSum;
				else
					break;
		}
		if(a[i]>a[i+1]){
			++down;
			for(j=i+1;j<=n;++j)
				if(a[j-1]>=a[j])
					++downSum;
				else
					break;		
		}
		if(a[i]==a[i+1]){
			int flag=0;
			for(j=i+1;j<=n;++j)
				if(a[j-1]<a[j]){
					flag=1;
					break;
				}
				else if(a[j-1]>a[j]){
					flag=-1;
					break;
				}
			if(flag==0)
				break;
			if(flag==1){
				++up;
				for(j=i+1;j<=n;++j)
					if(a[j-1]<=a[j])
						++upSum;
					else
						break;				
			}
			if(flag==-1){
				++down;
				for(j=i+1;j<=n;++j)
					if(a[j-1]>=a[j])
						++downSum;
					else
						break;					
			}
		}
		i=j-1;
	}
	double f1,f2;
	if(upSum==0)
		f1=0;
	else
		f1=upSum*1.0/up;
	if(downSum==0)
		f2=0;
	else
		f2=downSum*1.0/down;	
	printf("Nr values = %d:  %.6lf %.6lf\n",n,f1,f2);
}

int main()
{
	n=0;
	while(1){
		int x;
		scanf("%d",&x);
		if(x==0){
			if(n==0)
				break;
			deal();
			n=0;
		}
		else
			a[++n]=x;
	}
	return 0;	
} 


poj 1492 Up and Down Sequences 模拟计数