zoj 1338 Up and Down Sequences

316人阅读 评论(0)
#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;
}
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：488669次
• 积分：8072
• 等级：
• 排名：第2518名
• 原创：414篇
• 转载：22篇
• 译文：0篇
• 评论：55条
评论排行
最新评论