面向对象的程序设计_第二次作业 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; }
第二题
// 二 //// 测试样例: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) { char print[100] = { ' ' }; print[31] = '|'; cout << left << setw(10) << temp[i]; if (temp[i] > 0) { for (int j = 32; j < temp[i] + 32; ++j) { print[j] = '*'; } } else { for (int j = 30; j > 30 + temp[i]; --j) { print[j] = '*'; } } for (int k = 0; k < 100; ++k) cout << print[k]; cout << endl; } cout << endl; } void displayNum(int temp[], int n) { sort(temp, temp + 15); int before = temp[0]; int len = 1; int max_num = 1; int num = temp[0]; for (int i = 1; i < 15; ++i) { if (before == temp[i]) { len++; before = temp[i]; if (i == 14) { if (len > max_num) num = temp[i - 1]; max_num = max(len, max_num); } continue; } if (len > max_num) num = temp[i - 1]; max_num = max(len,max_num); len = 1; before = temp[i]; } cout << "出现次数最多的是" << num << "度,出现了" << max_num << "次"; } int main() { int temps[30]; inputTemps(temps, 30); displayTemps(temps, 30); displayNum(temps, 30); return 0; }
第三题
// 三 // 测试样例:2 3 BBWBEWW 4 WWWWBBEBB #include <iostream> #include <algorithm> using namespace std; int main() { int N = 0; cin >> N; for(int k = 1;k <= N; ++k){ char str[1000] = {' '}; int n = 0; cin >> n; int temp = 0; bool flag = 1; for (int i = 1; i <= 2 * n + 1; ++i) { cin >> str[i]; if (str[i] == 'E') temp = i; if (str[i] == 'B' && i < n) flag = 0; } cout << "结果_" << k << endl; if (flag) cout << "目标格局" << endl; else{ if (temp != 2 * n + 1) { swap(str[temp], str[temp + 1]); cout << str << endl; swap(str[temp], str[temp + 1]); if (temp != 2 * n) { swap(str[temp], str[temp + 2]); cout << str << endl; swap(str[temp], str[temp + 2]); if (temp != 2 * n - 1) { swap(str[temp], str[temp + 3]); cout << str << endl; swap(str[temp], str[temp + 3]); } } } if (temp != 1) { swap(str[temp], str[temp - 1]); cout << str << endl; swap(str[temp], str[temp - 1]); if (temp != 2) { swap(str[temp], str[temp - 2]); cout << str << endl; swap(str[temp], str[temp - 2]); if (temp != 3) { swap(str[temp], str[temp - 3]); cout << str << endl; swap(str[temp], str[temp - 3]); } } } } } }
作者:LightAc
出处:https://www.cnblogs.com/lightac/
联系:
Email: dzz@stu.ouc.edu.cn
QQ: 1171613053
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。