控制结构和函数

打印温度柱状图

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//输入n个温度
void inputTemps(int temp[], int n);
//显示月间温度的柱状图
void displayTemps(int temp[], int n);
//显示月间温度中的峰值
void displayPeaks(int temp[], int n);
//显示月间持续最久的温度
void displayFlat(int temp[], int n);
//最低温度
int minTemp(int temp[], int n);
//显示零下温度的柱状图
void subzeroTemps(int temp[], int n);
//求出现次数最多的温度及出现的次数
void maxTimes(int temp[], int n);

int main() {
	int temps[30] = { 11,-12,13,-11,11,11,11,10,-9,13,13,11,-16,14,15 };

	//inputTemps(temps, 30);
	//displayTemps(temps, 15);
	//displayPeaks(temps, 15);
	//displayFlat(temps, 15);
	subzeroTemps(temps, 15);
	//maxTimes(temps, 15);

	return 0;
}

void inputTemps(int temp[], int n) {
	cout << "Please input the tempratures:" << endl;
	for (int i = 0; i < n; i++) {
		cin >> temp[i];
	}
}

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

void displayPeaks(int temp[], int n) {
	cout << "显示峰值如下:" << endl;
	for (int i = 1; i < n - 1; i++) {
		if (temp[i - 1]<temp[i] && temp[i]>temp[i + 1]) {
			cout << "Max at day " << i + 1 << " is " << temp[i] << endl;
		}
	}
}

void displayFlat(int temp[], int n) {
	cout << "显示崮的长度如下:" << endl;
	int num = 1;
	int max_num = 1;
	for (int i = 1; i < n; i++) {
		if (temp[i - 1] == temp[i]) {
			num++;
			if (max_num < num) {
				max_num = num;
			}
		} else {
			num = 1;
		}
	}
	cout << "The length of longest flat is " << max_num << endl;
}

int minTemp(int temp[], int n) {
	int min_temp = temp[0];
	for (int i = 1; i < n; i++) {
		if (temp[i] < min_temp) {
			min_temp = temp[i];
		}
	}
	return min_temp;
}

void subzeroTemps(int temp[], int n) {
	cout << "显示温度柱状图如下:" << endl;
	int min_wide = abs(minTemp(temp, n)) + 3;
	for (int i = 0; i < n; i++) {
		cout.width(3);
		cout << temp[i];
		if (temp[i] < 0) {
			for (int j = 0; j < min_wide - abs(temp[i]); j++) {
				cout << " ";
			}
			for (int j = 0; j < abs(temp[i]); j++) {
				cout << "*";
			}
			cout << "|" << endl;
		} else {
			for (int j = 0; j < min_wide; j++) {
				cout << " ";
			}
			cout << "|";
			for (int j = 0; j < temp[i]; j++) {
				cout << "*";
			}
			cout << endl;
		}
	}
}

void maxTimes(int temp[], int n) {
	int times[100] = { 0 };
	int max_times = 0;
	int max_temp;
	for (int i = 0; i < n; i++) {
		times[temp[i]]++;
	}
	for (int i = 0; i < 100; i++) {
		if (times[i] > max_times) {
			max_times = times[i];
			max_temp = i;
		}
	}
	cout << "出现次数最多的是 " << max_temp << " 度,出现了 " 
		 << max_times << " 次。" << endl;
}

滑块游戏

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

void nextPattern(int n, char pattern[]);
void insertSort(int n, char next[10][10]);

int main() {
	int n;
	int num[10];
	char pattern[10][10];
	cout << "请输入格局数:";
	cin >> n;
	cout << "请输入格局:" << endl;
	for (int i = 0; i < n; i++) {
		cin >> num[i];
		for (int j = 0; j < 2 * num[i] + 1; j++) {
			cin >> pattern[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		cout << "结果_" << i + 1 << endl;
		nextPattern(num[i], pattern[i]);
	}
	return 0;
}

void nextPattern(int n, char pattern[]) {
	bool flag=true;
	int empty;
	int count = 0;
	char temp[10];
	char next[10][10];

	for (int i = 0, j = 0; i < 2 * n + 1; i++) {
		if (pattern[i] != 'E') {
			temp[j++] = pattern[i];
		} else {
			empty = i;
		}
	}
	for (int i = 0; i < 2 * n - 1; i++) {
		if (temp[i] < temp[i + 1]) {
			flag = false;
			break;
		}
	}
	if (flag) {
		cout << "目标格局!" << endl;
	} else {
		for (int i = empty + 3; i >= empty - 3; i--) {
			if ((i != empty) && i >= 0 && i <= 2 * n) {
				strcpy(temp, pattern);
				temp[empty] = temp[i];
				temp[i] = 'E';
				strcpy(next[count], temp);
				count++;
			}
		}
		insertSort(count, next);
		for (int i = 0; i < count; i++) {
			cout << next[i] << endl;
		}
	}
}

void insertSort(int n, char next[10][10]) {
	char temp[10];
	for (int i = 0; i < n - 1; i++) {
		int k = i;
		for (int j = i + 1; j < n; j++) {
			if (strcmp(next[k], next[j]) > 0) {
				k = j;
			}
		}
		if (k != i) {
			strcpy(temp, next[i]);
			strcpy(next[i], next[k]);
			strcpy(next[i], temp);
		}
	}
}
posted @ 2022-08-30 20:43  catting123  阅读(42)  评论(0)    收藏  举报