面向对象的程序设计_第二次作业 3月19日

第一题

//	一 
//	测试样例:11 12 13 11 11 11 11 10 9 13 13 11 16 14 15
//	测试样例:11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;

//	输入n个温度
void inputTemps(int temp[], int n) {
	cout << "Please input the temperatures:" << endl;
	for (int i = 0; i < 15; ++i)
		cin >> temp[i];
	cout << endl;
}

//	显示柱状图
void displayTemps(int temp[], int n) {
	cout << "显示柱状图如下:" << endl;
	for (int i = 0; i < 15; ++i) {
		cout << left << setw(10) << i + 1;
		for (int j = 0; j < temp[i]; ++j)
			cout << '*';
		cout << endl;
	}
	cout << endl;
}

//	显示月间温度中的所有峰值
void displayPeaks(int temp[], int n) {
	cout << "显示峰值如下:" << endl;
	bool flag = 0;
	for (int i = 1; i < 14; ++i)
		if (temp[i] > temp[i - 1] && temp[i] > temp[i + 1]) {
			cout << "Max at day " << i + 1 << " is " << temp[i] << endl;
			flag = 1;
		}
	if (!flag)
		cout << "没有峰值" << endl;
	cout << endl;
}

//	显示月间持续最久的温度
void displayFlat(int temp[], int n) {
	cout << "显示崮的长度如下:" << endl;
	int max_len = 1;
	int length = 1;
	int before = temp[0];
	for (int i = 1; i < 15; ++i) {
		if (before == temp[i]) {
			++length;
			before = temp[i];
			if(i == 14)	//	特殊极端情况的一个判定
				max_len = max(length, max_len);
			continue;
		}
		before = temp[i];
		max_len = max(length, max_len);
		length = 1;
	}
	cout << "The length of longest flat is " << max_len << endl;
}

//	主函数
int main() {
	int temps[30];

	inputTemps(temps, 30);
	displayTemps(temps, 30);
	displayPeaks(temps, 30);
	displayFlat(temps, 30);

	return 0;
}

猜你喜欢

转载自www.cnblogs.com/lightac/p/10560549.html
今日推荐