c++ 期中阶段练习

mac2025-12-23  8

大概思路:先求出第 i 行第 j 列的格左相邻的 0 的个数(包含自身) 然后把它当做矩形右下角来往上求最大的宽,求最大面积

#include <iostream> using namespace std; int min(int a, int b) { return a < b ? a : b; }; int max(int a, int b) { return a > b ? a : b; }; int main() { int m, n; cin >> m >> n; int a[20][20];//在a[i][j]左边有多少个连续0 for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { cin >> a[i][j]; if (a[i][j] == 0) a[i][j] = 1; else a[i][j] = 0; } for (int i = 0; i < m; i++) for (int j = 1; j < n; j++) { if (a[i][j] == 1) a[i][j] = a[i][j - 1] + 1; } //得出的a[i][j]相邻的0有多少个(含自身) int h, w;//长宽 int area = 1;//最大面积 for(int i=0;i<m;i++) for (int j = 0; j < n; j++) { if (a[i][j] != 0) { //求该行最大面积 w = a[i][j]; h = 1; area = max(area, h * w); for (int k = i - 1; k >= 0; k--) { //以该点为右下角往上算最大面积 if (a[k][j] == 0) break; w = min(w, a[k][j]); h++; area = max(area, w * h); } } } cout << area << endl; }

测试数据: 14 If the voltage is U=200V and the current is I=4.5A, which power is generated? A light-bulb yields P=100W and the voltage is U=220V. Compute the electrical current, please. bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage? A lightning strike with I=2A and P=2.5MW. Figure out the voltage. A big Transistor has a current of I=1mA and a voltage of U=700V. What power is consumed? Voltage U=1000kV – Ok. Current I=50A – Ok. Gimme the power! The 1st case is AV: this means I=12A, U=15V and P should be calculated. The 2nd case is AW: this means I=12A, P=15W and U should be calculated. The 3rd case is VA: this means U=12V, I=15A and P should be calculated. The 4th case is VW: this means U=12V, P=15W and I should be calculated. The 5th case is WA: this means P=12W, I=15A and U should be calculated. The 6th case is WV: this means P=12W, U=15V and I should be calculated. The modifiers for the voltage U=1MV and current I=1kA are leading to what power? Fractal values for voltage U=1.111kV and current I=1.111kA are leading to what power?

#include <iostream> #include<iomanip> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { char ch; cin.get(ch); long double U=0, P=0, I=0; while (cin.get(ch)) { if (ch == 'U') { cin.get(ch); if (ch != '=') continue; cin >> U; //cout << U << endl; cin.get(ch); if (ch == 'V') continue; else if (ch == 'm') U *= 0.001; else if (ch == 'k') U *= 1000; else if (ch == 'M') U *= 1000000; } else if (ch == 'P') { cin.get(ch); if (ch != '=') continue; cin >> P; //cout << P << endl; cin.get(ch); if (ch == 'W') continue; else if (ch == 'm') P *= 0.001; else if (ch == 'k') P *= 1000; else if (ch == 'M') P *= 1000000; } else if (ch == 'I') { cin.get(ch); if (ch != '=') continue; cin >> I; //cout << I << endl; cin.get(ch); if (ch == 'A') continue; else if (ch == 'm') I *= 0.001; else if (ch == 'k') I *= 1000; else if (ch == 'M') I *= 1000000; } if (ch == '\n') break; else continue; } long double x; cout << "Problem #"<<i+1 << endl; if (U == 0) { x = P / I; //cout << "Problem #3" << endl; cout << "U=" << setiosflags(ios::fixed | ios::showpoint) <<setprecision(2)<<x<< "V" << endl; cout << endl; } else if (P == 0) { x = U * I; //cout << "Problem #1" << endl; cout << "P=" << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << x << "W" << endl; cout << endl; } else if (I == 0) { x = P / U; //cout << "Problem #2" << endl; cout << "I=" << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << P / U << "A" << endl; cout << endl; } } }

大概思路:遍历a[i],从它开始遍历之前和之后的数组,如果比a[i]小就结束,大就宽度+1 需要考虑的情况比较多

#include<iostream> using namespace std; int max(int a, int b) { return a > b ? a : b; } int min(int a, int b) { return a < b ? a : b; } int main() { int n; cin >> n; int a[9999]; cin >> a[0]; int minnum = a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; minnum = min(minnum,a[i]); } int area=0; for (int i = 0; i < n; i++) { area = max(area, a[i]); int w=1; for (int j = i-1; j >= 0; j--) { if (a[j] < a[i]) { break; } else w++; } for (int j = i + 1; j < n; j++) { if (a[j] < a[i]) { //area = max(area, a[i] * (j - 1 - i)); break; } else w++; //area = max(area, a[i] * (j - i+1)); } area = max(area, a[i] * w); } area = max(area, minnum * n); cout << area << endl; }

大概思路:排序,差最大的地方就是分界点

#include<iostream>; using namespace std; int main() { //繁殖率 int n; cin >> n; long long start_num, end_num; double rate[101]; int id[101]; for (int i = 0; i < n; i++) { cin >> id[i] >> start_num >> end_num; rate[i] = end_num /start_num; } //从大到小排序 for (int i = 0; i < n-1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (rate[j] < rate[j + 1]) { swap(rate[j], rate[j + 1]); swap(id[j],id[j+1]); } } } //求AB分界线,即求最大的差 double maxdiff=0; int index=0; for (int i = 0; i < n-1; i++) { if ((rate[i] - rate[i + 1]) > maxdiff) { maxdiff = rate[i] - rate[i + 1]; index = i; } } cout << index + 1 << endl; for (int i = index ; i >= 0; i--) cout << id[i] << endl; cout << n - index - 1 << endl; for (int i = n-1; i >=index + 1; i--) cout << id[i] << endl; }

这个真的是 令人迷惑 观察每个反斜对角的i+j是依次递增的,没有蛇形矩阵复杂。。。

#include<iostream>; using namespace std; int main() { int row, col; cin >> row >> col; int a[99][99]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cin >> a[i][j]; } } for (int k = 0; k <= row-1+col-1; k++) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i + j == k) cout << a[i][j] << endl; } } } }

就是套路,闰年判断类问题

#include <iostream> using namespace std; int date(int year, int month, int day) { int count = -2; for (int i = 2000; i < year; i++) { if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) count += 366; else count += 365; } for (int k = 1; k < month; k++) switch (k) { case 1:case 3:case 5:case 7:case 8:case 10:case 12:count += 31; break; case 2: { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) count += 29; else count += 28; break; } case 4:case 6:case 9:case 11:count += 30; break; } count += day; return count; }; int main() { int n; cin >> n; string num; for (int i = 0; i < n; i++) { int year, month, day; char ch; cin >> year >> ch >> month >> ch >> day; cin.get(ch);//换行符 while (cin.get(ch)) { if (ch == '\n') break; num += ch; } //cout << year << month << day; int k = date(year, month, day) % 7; int x = num[num.length() - 1]-'0'; switch (k) { case 1: { if (x == 1 || x == 6) cout << "yes" << endl; else cout << "no" << endl; break; } case 2: { if (x == 2 || x == 7) cout << "yes" << endl; else cout << "no" << endl; break; } case 3: { if (x == 3 || x == 8) cout << "yes" << endl; else cout << "no" << endl; break; } case 4: { if (x == 4 || x == 9) cout << "yes" << endl; else cout << "no" << endl; break; } case 5: { if (x == 5 || x == 0 || x > 9) cout << "yes" << endl; else cout << "no" << endl; break; } case 6:case 0:cout << "no" << endl; break; } } }

#include<iostream>; #include<iomanip> using namespace std; int main() { int grid[22];//格子数 int n; cin >> n; grid[0] = 1; grid[n + 1] = 0; int count = 0; for(int i=0;i<n;i++) { //输入中间格子 cin >> grid[i + 1]; } int j = 0; while (j < n + 1) { if (grid[j] != 0) { count ++; int temp = j; j = j + grid[j]; if (j < 0) j = 0; /*else if (j > n+1) count -= j - n-1;*/ grid[temp] = 0; } else { count += 2; j++; } } cout << count << endl; }

思路:转换为开始时间的增加,当开始时间大于结束时间时,结束计数

#include<iostream> using namespace std; int main() { int n; cin >> n; int start = 60,end=180; int patient[22]; int count = 0; for (int i = 0; i < n; i++) { cin >> patient[i]; } for(int i=0;i<n-1;i++) for (int j = 0; j < n - 1 - i; j++) { if (patient[j] > patient[j + 1]) swap(patient[j], patient[j+1]); } for (int i = 0; i < n; i++) { if (patient[i]< start) { start += 10; count++; } else { start = patient[i] + 10; count++; } if (start > end) { count--; break; } } cout << count << endl; }

贪心算法 参考一下这个代码

#include<iostream> #include<iomanip> using namespace std; int main() { int n; while (cin >> n) { long long max = 0; long long sum = 0; long long x; for (int i = 0; i < n; i++) { cin >> x; if (x > max) max = x; sum += x; } int other = sum - max; if (other < max) { cout << setiosflags(ios::fixed|ios::showpoint)<<setprecision(1)<<(other * 1.0) << endl; } else { cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(1) << ((other - max) * 1.0 / 2 + max) << endl; } } }
最新回复(0)