暴力模拟入门
P1038 小红书-2022.9.23-零件组装
#include <bits/stdc++.h>
#include <cstdint>
using namespace std;typedef long long LL;
const int N = 100001;
int num[4];
LL d;
vector<vector<LL>> v(4, vector<LL>(N));int main()
{for(int i=0; i<4;i++)cin >> num[i];cin >> d;for(int i=0; i<4; i++){for(int j=0;j<num[i]; j++){cin>>v[i][j];}}int res = INT_MAX;for(int i=0; i<4; i++){int count = 0;//每种木头可以做木凳的最小值for(int j=0;j<num[i]; j++){if(v[i][j] >= d) count++;//cout << "v[i][j] = " <<v[i][j] << "\tcount = " << count << endl;}res = min(res, count);}cout << res << endl;return 0;
}
P1107 腾讯音乐-2023.3.23-第三题-塔子的签到题
#include <iostream>
using namespace std;
typedef long long LL;int main()
{string str;cin >> str;LL len = str.length();//str = "AasdxXxdxaCcCa";int res = 0;for (LL i = 1; i <= len; i++){//cout << "str[i-1] = " << str[i-1]-'A'<<endl;//cout << "str[i-1] = " << str[i-1] << "\tstr[i] = " << str[i] << endl;if(abs(str[i]-str[i-1]) == 32 || (abs(str[i]-str[i-1])==0)) res++;}cout << res << endl;return 0;
}
暴力模拟简单
P1128 腾讯音乐-2023.03.26-第一题-塔子哥考试
计算一下待测答案有多少个在标准答案出现过即可。根据大小关系比就行。
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;const int N10 = 1e5 + 10;
int n;
vector<string> v1(N10);
vector<string> v2(N10);int getScores(string chioce, string answer)
{unordered_set<char> s1, s2;for(char &ch1 : chioce) s1.insert(ch1);for(char &ch2 : answer) s2.insert(ch2);bool flag=true;for(auto &ch : s1){if(s2.count(ch) == 0) flag = false;}if(flag){if(chioce.size() == answer.size()) return 3;return 1;}return 0;
}int main()
{cin >> n;for (int i = 0; i < n; i++)cin >> v1[i];for (int i = 0; i < n; i++)cin >> v2[i];int res = 0;for(int i=0; i<n; i++){res += getScores(v1[i], v2[i]);}cout << res << endl;return 0;
}
P1030 华为od-2022.11.27-平均像素值
#include<bits/stdc++.h>
#include<iostream>
using namespace std;int v[102];int get(int a)
{if(a < 0) return 0;if(a > 255) return 255;return a;
}int main()
{int res=0,mi=100000000;int index = 0;while(cin >> v[index])index++;//注意到整数k范围不大,最低不会低于-255,最高不会到255,//所以直接枚举这个范围内的每个数作为k,然后得到最优解即可。for(int i=-255; i<= 255; i++){int sum = 0;for(int j=0; j<index; j++){sum += get(v[j] + i);//新像素值求和}int nowsum = abs(sum - 128 * index);//差值if(nowsum < mi){mi = nowsum;res = i;}}cout << res << endl;return 0;
}