A题 P5660 数字游戏
https://www.luogu.com.cn/problem/P5660
循环遍历到数组末尾,统计字符1的个数即可。
#include<iostream>
using namespace std;
string s;
int cnt;
int main() {
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] - '0' & 1) {
cnt++;
}
}
cout << cnt << endl;
return 0;
}
B题 P1739 表达式括号匹配
https://www.luogu.com.cn/problem/P1739
使用STL stack栈来使用表达式括号匹配,左括号时压入栈,右括号将栈顶元素弹出,特判点要注意栈没有元素时要输出NO并结束。
stack<类型> 变量
s.push()放入栈顶
s.pop()删除栈顶元素
s.top()返回栈顶元素,但不会删除
s.empty()判断栈是否为空
s.size()返回栈中元素的个数
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(){
stack<char> s;
string a;
cin >> a;
for (int i = 0; i < a.size(); i++){
if (a[i] == '(') {
s.push(a[i]);
}
if (a[i] == ')'){
if (s.empty()){
cout << "NO" << endl;
return 0;
}
s.pop();
}
}
if (s.empty()) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
return 0;
}
C题 P1179 数字统计
https://www.luogu.com.cn/problem/P1179
掌握分离多位数统计字符2既可。
#include<iostream>
using namespace std;
int L, R;
int cnt;
void check(int n) {
while (n > 0) {
if (n % 10 == 2) {
cnt++;
}
n = n / 10;
}
}
int main() {
cin >> L >> R;
while (L <= R) {
check(L);
L++;
}
cout << cnt << endl;
return 0;
}
D题 P2010 回文日期
https://www.luogu.com.cn/problem/P2010
枚举所有的月和日的既可,求出后四位然后将其反过来作为前四位一定是回文串,0229反过来是9220,9220是闰年,将在范围内的统计既可。
#include<iostream>
using namespace std;
int i, j, n, m, a, b, c, sum, ans;
int s[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int main()
{
cin >> n >> m;
for (i = 1; i <= 12; i++)//枚举月和日
for (j = 1; j <= s[i]; j++)
{
c = (j % 10) * 1000 +
(j / 10) * 100 +
(i % 10) * 10 +
(i / 10);//算出前四位。
sum = c * 10000 + i * 100 + j;//算出整个日期
if (sum<n || sum>m) continue;
ans++;//统计
}
cout << ans << endl;
return 0;
}
E题 P1051 谁拿了更多的奖学金
https://www.luogu.com.cn/problem/P1051
可以用结构体保存数据,然后用sort模板排序,输出最大值既可。(其他方法看群里PDF)
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int n;
long long money;
struct student {
string name;
int avg;
int cls;
bool bgb;
bool xibu;
int lunwen;
long long sumMoney;
int id;
}st[105];
bool cmp(student s1, student s2) {
if (s1.sumMoney != s2.sumMoney)
return s1.sumMoney > s2.sumMoney;
return s1.id < s2.id;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
char t1;
char t2;
cin >> st[i].name;
cin >> st[i].avg;
cin >> st[i].cls;
cin >> t1;
cin >> t2;
cin >> st[i].lunwen;
if (t1 == 'Y') {
st[i].bgb = true;
}
else {
st[i].bgb = false;
}
if (t2 == 'Y') {
st[i].xibu = true;
}
else {
st[i].xibu = false;
}
if (st[i].avg > 80 && st[i].lunwen >= 1) {
st[i].sumMoney += 8000;
}
if (st[i].avg > 85 && st[i].cls > 80) {
st[i].sumMoney += 4000;
}
if (st[i].avg > 90) {
st[i].sumMoney += 2000;
}
if (st[i].avg > 85 && st[i].xibu == true) {
st[i].sumMoney += 1000;
}
if (st[i].cls > 80 && st[i].bgb == true) {
st[i].sumMoney += 850;
}
money += st[i].sumMoney;
st[i].id = i;
}
sort(st + 1, st + n + 1, cmp);
cout << st[1].name << endl;
cout << st[1].sumMoney << endl;
cout << money << endl;
return 0;
}
F题 P1605 迷宫
https://www.luogu.com.cn/problem/P1605
DFS模板题,用一个二维数组模拟4个方向,用一个数组标记障碍点。
注意点:
- 判断条件要弄清楚 障碍物要处理 走过的点要进行标记 走完之后要回溯,将这个点还原为开始状态
一定要先排除不能走的点,再判断是否到终点(测试时我 wa 了一个点)
#include<iostream>
#include<cstdio>
using namespace std;
const int N = 5 + 1;
const int Maxn = 30;
int maps[N][N];
bool mapFlag[N][N];
int mapx[Maxn], mapy[Maxn];
int f[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
int sx, sy, fx, fy;
int n, m, t;
int cnt;
void dfs(int x, int y) {
if (x == fx && y == fy) {
cnt++;
return;
}
for (int i = 0; i < 4; i++) {
int dx = 0, dy = 0;
dx += x + f[i][0];
dy += y + f[i][1];
if (dx<1 || dy<1 || dx>n || dy>m) {
continue;
}
if (maps[dx][dy] == 0 && mapFlag[dx][dy] != true) {
maps[dx][dy] = 1;
dfs(dx, dy);
maps[dx][dy] = 0;
}
}
}
int main() {
cin >> n >> m;
cin >> t;
cin >> sx >> sy >> fx >> fy;
memset(mapFlag, false, sizeof(mapFlag));
for (int i = 1; i <= t; i++) {
cin >> mapx[i];
cin >> mapy[i];
mapFlag[mapx[i]][mapy[i]] = true;
}
maps[sx][sy] = 1;
dfs(sx, sy);
cout << cnt << endl;
return 0;
}
G题 P1223 排队接水
https://www.luogu.com.cn/problem/P1223
我们需要用到贪心的策略,让时间少的排前面接水,所以我们需要进行排序,排完之后我们使用前缀和求出所有人的等待时间,最后除以所有人即时平均等待时间。
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1000 + 5;
struct student {
int time;
int id;
}stu[N];
int n;
double times;
long long k[N];
bool cmp(student s1, student s2) {
return s1.time < s2.time;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> stu[i].time;
stu[i].id = i;
}
sort(stu + 1, stu + n + 1, cmp);
for (int i = 1; i <= n; i++) {
cout << stu[i].id << " ";
k[i] = stu[i].time + k[i - 1];
times += k[i - 1];
}
cout << endl;
printf("%.2f\n", times / n);
return 0;
}
H题 P1873 砍树
https://www.luogu.com.cn/problem/P1873
二分答案的思想,通过二分法找到最小的最大值。
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const int Max = 2e9;
ll n, m;
ll s[maxn];
bool check(ll x) {
ll sum = 0;
for (int i = 1; i <= n; i++) {
if (s[i] - x > 0) {
sum += s[i] - x;
}
}
if (sum >= m) {
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
ll ls = 1, rs = Max + 1;
while (ls + 1 < rs) {
ll mid = (ls + rs) / 2;
if (!check(mid)) {
rs = mid;
}
else {
ls = mid;
}
}
cout << ls << endl;
return 0;
}