zoj 1338 Up and Down Sequences

#include "iostream"
#include "stdio.h"
#include "iomanip"
using namespace std;

int main()
{
	int num[30], len, i, uptimes, downtimes, midtimes, sum1, sum2;
	bool up, down;
	while (cin >> num[0] && num[0])
	{
		len = 1;
		while (cin >> num[len] && num[len])
			len++;

		up = false, down = false;
		uptimes = downtimes = midtimes = sum1 = sum2 = 0;
		for (i = 1; i < len; i++)
		{
			if (up && num[i] >= num[i-1])
				sum1++;
			else if (down && num[i] <= num[i-1])
				sum2++;
			else if (up && num[i] < num[i-1])
			{
				up = false;
				down = true;
				downtimes++;
				sum2++;
			}
			else if (down && num[i] > num[i-1])
			{
				down = false;
				up = true;
				uptimes++;
				sum1++;
			}
			else if (!up && !down && num[i] > num[i-1])
			{
				up = true;
				sum1 += midtimes;
				midtimes = 0;
				sum1++;
				uptimes++;
			}
			else if (!up && !down && num[i] < num[i-1])
			{
				down = true;
				sum2 += midtimes;
				midtimes = 0;
				sum2++;
				downtimes++;
			}
			else if (!up && !down && num[i] == num[i-1])
				midtimes++;
		}
		cout.setf(ios::fixed);
		cout << "Nr values = " << len << ":  ";
		if (uptimes == 0)
			cout << "0.000000 ";
		else 
			cout << setprecision(6) << double(sum1) / double (uptimes) << " ";
		if (downtimes == 0)
			cout << "0.000000" << endl;
		else
			cout << setprecision(6) << sum2 / (double)downtimes << endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值