这里写目录标题
- 链接
- 题目
- 代码
- 大佬解答
- string提取索引转换为值
链接
P8752 [蓝桥杯 2021 省 B2] 特殊年份
题目
代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
#include <climits> // 包含INT_MAX常量
#include <cctype>using namespace std;vector<int> years(5);
int num;int check(int n) {int a, b, c, d;d = n % 10;n /= 10;c = n % 10;n /= 10;b = n % 10;n /= 10;a = n;if (a == c && b + 1 == d)return 1;elsereturn 0;
}int main() {for (int i = 0; i < 5; i++) {cin >> years[i];if (check(years[i]))num++;}cout << num;return 0;
}
大佬解答
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
#include <climits> // 包含INT_MAX常量
#include <cctype>
using namespace std;int main() {char a, b, c, d;int nums = 0;for (int i = 0; i < 5; i++) {cin >> a >> b >> c >> d;//用于读取四位数的每一位if (a == c && b + 1 == d)nums++;}cout << nums;return 0;
}
string提取索引转换为值
cin >> s; cout << s[0] - '0' << endl;
输入:2025
输出:2