基础算法
枚举
将问题中每个解都释放出来,进行验证和比较,以找出最优解和所有解。
1.特别数的和 OJ:191

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll ans = 0; // 累计和
int n; // 数字个数
// 检查数字是否满足条件
int cheak(int x) {
while (x) {
int t = x % 10;
if (t == 2 || t == 0 || t == 1 || t == 9)
return 1;
x /= 10;
}
return 0;
}
int main() {
cin >> n; // 读取数字个数
for (int i = 1; i <= n; i++) {
if (cheak(i)) // 如果数字满足条件,将其累加到结果中
ans += i;
}
cout << ans << endl; // 输出结果
return 0;
}
2.反倍数 OJ:152

#include <iostream>
using namespace std;
int a, b, c; // 定义整型变量 a、b、c
bool f(int x) {
// 判断是否满足 x 除以 a、b、c 的余数均不为 0
return x % a != 0 && x % b != 0 && x % c != 0;//&&是逻辑与,相当于且,需同时满足
}
int main() {
// 定义整型变量 n 和 sum
int n, sum;
cin >> n;
cin >> a >> b >> c;
for (int i = 1; i <= n; i++) {
// 如果 i 满足函数 f 的条件,就把 sum 加 1
if (f(i)) {
sum++;
}
}
cout << sum << endl;
return 0;
}
3.找到最多的数 OJ:3227

#include <bits/stdc++.h> // 包含 STL 库的头文件
using namespace std; // 使用 std 命名空间
const int N = 1e6 + 9; // 定义常量 N,表示数组的大小。
int a[N]; // 定义一个大小为 N 的整型数组
int main() {
int n, m; // 定义整型变量 n 和 m,分别表示行数和列数
cin >> n >> m; // 输入行数和列数
int k = 0; // 初始化计数器 k 为 0
for (int i = 1; i <= n; i++) { // 遍历每一行
for (int j = 1; j <= m; j++) { // 遍历每一列
cin >> a[k++]; // 从标准输入中读取一个数,并将其存储在数组 a 中
}
}
sort(a, a + k); // 对数组 a 进行排序
cout << a[k / 2]; // 输出排序后数组 a 中的中间元素
return 0;
}
const 变量指的是,此变量的值是只读的,不应该被改变。
双层for循环遍历整个矩阵。
使用STL中的sort函数。
sort()的使用方法
在C++中使用sort()函数需要使用
#include <bits/stdc++.h>
头文件
sort()基本使用方法
sort()函数可以对给定区间所有元素进行排序。它有三个参数sort(begin, end, cmp),其中begin为指向待sort()的数组的第一个元素的指针,end为指向待sort()的数组的最后一个元素的下一个位置的指针,cmp参数为排序准则,cmp参数可以不写,如果不写的话,默认从小到大进行排序。
具体请看这里。
4.小蓝的漆房 OJ:3272(有代码无注释)


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;//10的5次方加10,即10010
int a[N] , b[N];
signed main()
{
int T = 1;
cin >> T;
while(T --){
int n , k , res = 0x3f3f3f3f;//一般dfs或者dp求最小值的问题中间会出现一些不存在的情况,一般会采用一种方法就是返回一个很大的值,这样去最小的时候就不会取到这种情况了
cin >> n >> k;
for(int i = 1 ; i <= n ; i ++) cin >> a[i];
for(int i = 1 ; i <= 60 ; i ++){
int cnt = 0;
for(int j = 1 ; j <= n ; j ++) b[j] = a[j];
for(int j = 1 ; j <= n ; j ++){
if(b[j] != i){
for(int h = j ; h <= j + k - 1 ; h ++) b[h] = i;
j = j + k - 1;
cnt ++ ;
}
}
res = min(res , cnt);
}
cout << res << '\n';
}
return 0;
}
模拟
一般由较多简单但不好处理的问题组成,会经常写比较多的小函数辅助解题
例如int和string的相互转换,回文串判断,日期转换,各种条件判断等。
1.扫雷 OJ:549


#include <iostream> // 包含输入输出流的头文件
using namespace std; // 使用 std 命名空间
int n, m; // 定义整型变量 n 和 m,表示矩阵的行数和列数
int d[100][100] = {0}; // 定义一个二维整数数组 d,用于存储矩阵的数据
int main() {
cin >> n >> m; // 输入矩阵的行数和列数
for (int i = 0; i < n; i++) { // 遍历矩阵的每一行
for (int j = 0; j < m; j++) { // 遍历矩阵的每一列
cin >> d[i][j]; // 把矩阵中每个元素的值输入数组
}
}
for (int i = 0; i < n; i++) { // 遍历矩阵的每一行
for (int j = 0; j < m; j++) { // 遍历矩阵的每一列
int count = 0; // 定义整型变量 count,用于统计满足条件的元素个数
if (d[i][j] == 1) { // 如果当前元素的值为 1
cout << 9 << " "; // 输出 9
}
else { // 否则
for (int x = i - 1; x <= i + 1; x++) { // 遍历当前元素所在行的上一行和下一行
for (int y = j - 1; y <= j + 1; y++) { // 遍历当前元素所在列的左一列和右一列
if (x >= 0 && y >= 0 && x < n && y < m && d[x][y] == 1) { // 检查坐标是否在矩阵范围内,并且对应元素的值为 1
count++; // 满足条件的元素个数加 1
}
}
}
cout << count << " "; // 输出满足条件的元素个数
}
}
cout << endl; // 输出换行符
}
return 0; // 程序正常结束,返回 0
}
2.灌溉 OJ:551


#include <iostream> // 包含输入输出流的头文件
using namespace std; // 使用 std 命名空间
const int MAX_N = 100; // 定义常量 MAX_N,表示矩阵的最大大小
int a[MAX_N][MAX_N], b[MAX_N][MAX_N]; // 定义整型二维数组 a 和 b
int main() {
int n, m, t; // 定义整型变量 n、m 和 t,分别表示矩阵的行数、列数和灌溉次数
cin >> n >> m >> t; // 输入矩阵的行数、列数和灌溉次数
for (int i = 0; i < t; ++i) { // 遍历灌溉次数
int x, y; // 定义整型变量 x 和 y,表示当前灌溉的位置
cin >> x >> y; // 输入当前灌溉的位置的行号和列号
a[x - 1][y - 1] = 1; // 将当前灌溉的位置标记为已灌溉
}
int k; // 定义整型变量 k,表示剩余的灌溉次数
cin >> k;
while (k--) { // 当还有灌溉次数时,执行循环
for (int i = 0; i < n; i++) { // 遍历矩阵的每一行
for (int j = 0; j < m; j++) { // 遍历矩阵的每一列
if (a[i][j] == 1) { // 如果当前位置已经被灌溉
if (i - 1 >= 0) b[i - 1][j] = 1; // 将当前位置左上角的位置标记为已灌溉
if (i + 1 < n) b[i + 1][j] = 1; // 将当前位置右上角的位置标记为已灌溉
if (j - 1 >= 0) b[i][j - 1] = 1; // 将当前位置左下角的位置标记为已灌溉
if (j + 1 < m) b[i][j + 1] = 1; // 将当前位置右下角的位置标记为已灌溉
b[i][j] = 1; // 将当前位置标记为已灌溉
}
}
}
for (int i = 0; i < n; ++i) { // 遍历矩阵的每一行
for (int j = 0; j < m; ++j) { // 遍历矩阵的每一列
a[i][j] = b[i][j]; // 将 b 数组中的值复制到 a 数组中
b[i][j] = 0; // 将 b 数组中的值重置为 0,以便下一次迭代使用
}
}
}
int ans = 0; // 定义整型变量 ans,表示被灌溉的位置的数量
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i][j] == 1) {
ans++; // 如果当前位置被灌溉,ans 加 1
}
}
}
cout << ans << '\n'; // 输出被灌溉的位置的数量
return 0; // 程序正常结束,返回 0
}
3.回文日期 OJ:498


#include <iostream>
using namespace std;
bool isLeap(int y){
return (y%4==0&&y%100!=0)||(y%400==0);
}
bool check(int year,int month,int day){//判断是否为合法日期
if(month>12||month==0) return false;
if(day>31) return false;
if(month==2){
if(isLeap(year)&&day>29)
return false;
if(!isLeap(year)&&day>28)
return false;
}
if(month==4||month==6||month==9||month==11){
if(day>30) return false;
}
return true;
}
int main()
{
int n,i;
cin>>n;
int a,b,c,d,e,f,g,h;//8位数字
int year,month,day;
bool flag=false;
for(i=n+1;i<=99999999;i++){
year=i/10000;
month=(i%10000)/100;
day=i%100;
a=i%10;
b=(i/10)%10;
c=(i/100)%10;
d=(i/1000)%10;
e=(i/10000)%10;
f=(i/100000)%10;
g=(i/1000000)%10;
h=(i/10000000)%10;
if(a==h&&b==g&&c==f&&d==e&&flag==false){
if(check(year,month,day)){
cout<<i<<endl;
flag=true;//只输出一个回文
}
}
if(a==h&&b==g&&c==f&&d==e&&a==c&&b==d){
if(check(year,month,day)){
cout<<i<<endl;
break;
}
}
}
return 0;
}
4.小蓝和小桥的挑战 OJ:3238


#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N] , t , n;
int main()
{
cin >> t;
while(t --)
{
cin >> n;
int sum = 0 , z = 0;
for(int i = 1 ; i <= n ; i ++)
{
cin >> a[i] , sum += a[i];
if(!a[i]) z ++;
}
sum += z;
if(sum == 0) cout << z + 1 << '\n';
else cout << z << '\n';
}
return 0;
}
5.DNA序列修正 OJ:3238


#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int a[N] , t , n;
int main()
{
cin >> t;
while(t --)
{
cin >> n;
int sum = 0 , z = 0;
for(int i = 1 ; i <= n ; i ++)
{
cin >> a[i] , sum += a[i];
if(!a[i]) z ++;
}
sum += z;
if(sum == 0) cout << z + 1 << '\n';
else cout << z << '\n';
}
return 0;
}
6.无尽的石头 OJ:3766


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAX = 1e6;
vector<ll> stones;
ll sum_digits(ll n) {
ll sum = 0;
while (n) {
sum += n % 10;
n /= 10;
}
return sum;
}
void preprocess() {
stones.push_back(1);
while (true) {
ll next = *--stones.end() + sum_digits(*--stones.end());
if (next <= MAX) {
stones.push_back(next);
} else {
break;
}
}
}
int main() {
preprocess();
int t;
cin >> t;
vector<ll> ans;
while (t--) {
int n;
cin >> n;
auto it = find(stones.begin(), stones.end(), n);
if (it != stones.end()) {
cout << it - stones.begin() << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
递归
函数直接或间接调用自身。
1.数的计算 OJ:760

#include<bits/stdc++.h>
using namespace std;
int alln(int n){
int sum=0,num=n/2;
if(n<=1)return 1;
else{
for(int i=0;i<=num;++i)
sum+=alln(i);
return sum;
}
}
int main(){
int m;
cin>>m;
cout<<alln(m);
}
2.计算函数值 OJ:


#include<bits/stdc++.h>
using namespace std;
int f(int x){
if(x==0)return 1;
if(x%2)return f(x-1)+1;
return f(x/2);
}
int main(){
int n;
cin>>n;
cout<<f(n);
return 0;
}
进制转换
1.进制 OJ:2489

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("%d",0X2021ABCD);
return 0;
}
2.Alice和Bob的爱恨情仇 OJ:3865


#include <iostream>
using namespace std;
long long ans,n,k,x;
int main()
{
cin>>n>>k;
while(n--)
{
cin>>x;
ans+=(x%2);
}
if(ans%2)
{
cout<<"Alice";
}
else
{
cout<<"Bob";
}
return 0;
}
前缀和
1.区间次方和 OJ:3382


#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
LL a[N];
LL b[N][10];
int n, m, l, r, k;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
for (int j = 1; j <= 5; j++)
b[i][j] = b[i - 1][j] + pow(a[i], j);
}
while (m--) {
cin >> l >> r >> k;
cout << (b[r][k] - b[l - 1][k]) % MOD << "\n";
}
return 0;
}
2.小郑的蓝桥平衡串 OJ:3419

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 2000;
int mp[N * 2];
int main() {
string s;
cin >> s;
int n = s.length();
int sum = 0;
int ans = 0;
memset(mp, -1, sizeof(mp));
mp[N] = 1;
for (int i = 0; i < n; i++) {
if (s[i] == 'L') {
sum++;
} else {
sum--;
}
if (mp[sum + N] != -1) {
ans = max(ans, i - mp[sum + N] + 1);
} else {
mp[sum + N] = i + 1;
}
}
cout << ans << endl;
return 0;
}
3.大石头的搬运工 OJ:3829


#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int n;
PII q[N];
LL pre[N], nex[N];
int main()
{
cin >> n;
for (int i = 1; i <= n; ++ i )
cin >> q[i].y >> q[i].x;
sort(q + 1, q + n + 1);
LL s = 0;
for (int i = 1; i <= n; ++ i )
{
pre[i] = pre[i - 1];
pre[i] += s * (q[i].x - q[i - 1].x);
s += q[i].y;
}
s = 0;
for (int i = n; i >= 1; -- i )
{
nex[i] = nex[i + 1];
nex[i] += s * (q[i + 1].x - q[i].x);
s += q[i].y;
}
LL res = 1e18;
for (int i = 1; i <= n; ++ i )
res = min(res, pre[i] + nex[i]);
cout << res << endl;
return 0;
}
4.最大数组和 OJ:3260



#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while(t--){
int n, k;
cin >> n >> k;
vector<ll> a(n), sum(n + 1, 0);
for(int i = 0;i < n;i++) cin >> a[i];
sort(a.begin(), a.end());
for(int i = 1;i <= n;i++) sum[i] = sum[i - 1] + a[i - 1];
ll ans = 0;
int pos = 0;
while(k >= 0){
ans = max(ans, sum[n - k] - sum[pos]);
pos += 2;
k--;
}
cout << ans << "\n";
}
return 0;
}
5.四元组问题 OJ:3416


#include <bits/stdc++.h>
using namespace std;
bool FoursNumberFind(vector<int>& nums) {
stack<int> st;
int n = nums.size(), k = INT_MIN, INF = INT_MAX;
//min_r[i] = min(nums[r]), i < r < n。
//表示第i个数(不包括第i个数)右边的最小值
vector<int> min_r(n, INF);
// 用前缀和方法求 min_r 数组
for (int i = n - 2; i >= 0; --i) {
min_r[i] = min(min_r[i + 1], nums[i + 1]);
}
//用单调栈求下标位 nums[c] < nums[a] < nums[b] 的情况
for(int i = 0; i < n; ++i){
// 下标 i 即为 c ,k 即为 nums[a]
if(nums[i] < k) { //如果存在 nums[c] < nums[a] < nums[b] 的情况
//判断 c 的右边是否 有比 nums[c] 小的数, 有则表示存在下标 d ,返回true
if(nums[i] > min_r[i]) return true;
}
// 如果栈不为空并且栈顶元素小于当前访问的元素
while(!st.empty() && st.top() < nums[i]) {
//需要找满足小于nums[b]的最大 k 值
k = max(k,st.top());
st.pop();
}
//压入栈顶,即为更新 nums[b]
st.push(nums[i]);
}
return false;
}
int main() {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
if (FoursNumberFind(nums)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
差分
1.区间更新 OJ:3291


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 1e7 + 10;
int num[maxn];
int vis[maxn];
int n, m;
int l, r, val;
int main( ){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i =0;i <= n+1; ++i) vis[i] = 0;
for(int i = 1;i <= n; ++i) scanf("%d",num+i);
while(m--){
scanf("%d%d%d",&l,&r,&val);
if(l>r) swap(l,r);
vis[l] += val;
vis[r+1] -= val;
}
for(int i = 1;i <= n; ++i){
vis[i] += vis[i-1];
num[i] += vis[i];
}
printf("%d",num[1]);
for(int i = 2;i <= n; ++i){
printf(" %d",num[i]);
}
puts("");
}
return 0;
}
2.小明的彩灯 OJ:1276


#include <iostream>
const int maxn=500005;
long long N[maxn]={0};
long long b[maxn]={0};
using namespace std;
int main()
{
int P,Q;
cin>>P>>Q;
for(int i=1;i<=P;i++){
cin>>N[i];
b[i]=N[i]-N[i-1];//定义差分和数组
}
while(Q>0){
long long l,r,x;
cin>>l>>r>>x;
b[l]+=x;
b[r+1]-=x;//对两端进行操作
Q--;
}
for(int i=1;i<=P;i++){
N[i]=b[i]+N[i-1];//最后回归原来数组
if(N[i]<0){
cout<<0<<" ";
}else
cout<<N[i]<<" ";
}
return 0;
}
3.肖恩的投球游戏 OJ:3693


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
const int N = 100010;
int n, q;
LL a[N], b[N];
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= q; ++i) {
int l, r, c;
cin >> l >> r >> c;
b[l] += c, b[r + 1] -= c;
}
for (int i = 1; i <= n; ++i) b[i] += b[i - 1];
for (int i = 1; i <= n; ++i) cout << a[i] + b[i] << " ";
return 0;
}
4.肖恩的投球游戏加强版 OJ:3694
5.泡澡



被折叠的 条评论
为什么被折叠?



