yzl_rex

对于算法,我只是一个草民!

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;
	}
}

阅读更多
文章标签: up include ios
个人分类: ZOJ
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

zoj 1338 Up and Down Sequences

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭