复盘1.
1.0日期统计 - 蓝桥云课 (lanqiao.cn)
#include
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f;
const int N = 3e4 + 5;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int a[110], ans = 0;
void solve() {
for (int i = 0; i < 100; i++)
cin >> a[i];
for (int i = 1; i <= 12; i++) {
for (int j = 1; j <= month[i]; j++) {
string sr = "2023";
if (i < 10)
sr += "0";
sr += to_string(i);
if (j < 10)
sr += "0";
sr += to_string(j);
int r = 0;
for (int k = 0; k < 100; k++) {
if (a[k] == sr[r] - '0') {
r++;
}
if (r == 8) {
ans++;
break;
}
}
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
2.顺子日期
#include
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f;
const int N = 20220000;
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, a[8];
void solve() {
int cn = 0;
for (int i = 1; i <= 12; i++) {
for (int j = 1; j <= month[i]; j++) {
int m = N + i * 100 + j;
int k = 0;
while (m) {
a[k] = m % 10;
m /= 10;
k++;//12345678->87654321
}
for (int l = 1; l < k - 1; l++) {
if (a[l - 1] - a[l] == 1 && a[l] - a[l + 1] == 1) {
cn++;
break;
}
}
}
}
cout << cn;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
3.将数组变为升序最少改变对数
#include
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f;
const int N = 3e4 + 5;
int a[N], b[N];
mapmp;
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
mp[a[i]] = i;
}
sort(a, a + n);
int cn = 0;
for (int i = 0; i < n; i++) {
if (a[i] != b[i]) {
mp[b[i]] = mp[a[i]];
b[mp[a[i]]] = b[i];
b[i] = a[i];
cn++;
}
}
cout << cn;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
4.辗转相除法求最大公约数
#include
using namespace std;
#define int long long
const int inf = 0x3f3f3f3f;
const int N = 3e4 + 5;
int gcd(int a, int b) {
int c;
while (c) {
c = a % b;
a = b;
b = c;
}
return a;
}
void solve() {
int a, b;
cin >> a >> b;
cout << gcd(a, b);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
5.队列Q (nowcoder.com)
#include
using namespace std;
const int N=1e5+5;
const int inf=0x3f3f3f3f;
#define int long long
int a[N],b[N];
bool cmp(int p,int q)
{
return b[p]>n;
for(int i=0;i>a[i];
b[a[i]]=i;
}
int m;
cin>>m;
int l=0,r=n;
while(m--)
{
string str;
cin>>str;
int o;
cin>>o;
if(str=="FIRST")
{
b[o]=--l;
}
else
{
b[o]=++r;
}
}
sort(a,a+n,cmp);
for(int i=0;i
6.指纹锁 (nowcoder.com)
#include
using namespace std;
const int N=3e4+5;
const int inf=0x3f3f3f3f;
#define int long long
int k;
struct cmp{
bool operator()(const int &a,const int &b)
{
if(abs(a-b)<=k)return false;
return ase;
void solve()
{
int m;
cin>>m>>k;
while(m--)
{
string str;
int x;
cin>>str>>x;
if(str=="add"){se.insert(x);}
else if(str=="del"){se.erase(x);}
else
{
if(se.find(x)!=se.end())cout<<"Yes"<
7.进制转换
十进制转换为m进制
1)
#include
using namespace std;
const int N = 3e4 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
stackst;
void solve() {
int n, m;
cin >> n >> m;
while (n) {
int a = n % m;
n /= m;
st.push(a);
}
while (!st.empty()) {
cout << st.top();
st.pop();
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
2)
#include
using namespace std;
const int N = 1e4 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve() {
int n, m;
cin >> n >> m;//n是十进制数,m是要转化成的进制
char str[220];
_itoa(n, str, m);
cout << str;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
二进制转换为十进制:
#include
using namespace std;
const int N = 1e4 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
char a[110], b[110];
void solve() {
cin >> a;
int sum = 0;
int len = strlen(a);
for (int i = 0; i < len; i++) {
b[i] = a[i] - '0';
if (b[i] == 1) {
sum += pow(n, len - i - 1);
}
}
cout << sum;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
8.最大区段和
#include
using namespace std;
const int N = 3e4 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve() {
int n;
cin >> n;
int a, b, sum = -inf;
for (int i = 0; i < n; i++) {
cin >> a;
if (i == 0)
b = a;
else
b = max(a + b, a);
sum = max(sum, b);
}
cout << sum;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
9.问题 - 1276 (hdu.edu.cn)
#include
using namespace std;
const int N = 3e4 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve()
{
int n;
cin>>n;
while(n--)
{
vectorve;
int x;
cin>>x;
for(int i=1;i<=x;i++)
{
ve.push_back(i);
}
int flag=1;
while(ve.size()>3)
{
if(flag==1)
{
flag=2;
for(int i=1;i<=x;i++)
{
ve.erase(ve.begin()+i,ve.begin()+1+i);
}
}
else
{
flag=1;
for(int i=2;i<=x;i+=2)
{
ve.erase(ve.begin()+i,ve.begin()+i+1);
}
}
}
for(int i=0;i
10.问题 - 2094 (hdu.edu.cn)
#include
using namespace std;
const int N = 3e4 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve()
{
int n;
while(cin>>n)
{
if(n==0)break;
setse1,se2;
string str1,str2;
while(n--)
{
cin>>str1>>str2;
se1.insert(str1),se1.insert(str2);
se2.insert(str2);
}
if(se1.size()-se2.size()==1)cout<<"Yes"<
11.C-数组段数_牛客小白月赛86 (nowcoder.com)
#include
using namespace std;
const int N = 3e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
int a[N];
void solve()
{
int n,m;
cin>>n>>m;
int b,c=-1,sum=0;
for(int i=1;i<=n;i++)
{
cin>>b;
if(b!=c)
{
sum++;
c=b;
}
a[i]=sum;
}
int l,r;
while(m--)
{
cin>>l>>r;
cout<
12.G-堆_集美大学"第15届蓝桥杯大赛(软件类)"校内选拔赛 (nowcoder.com)
#include
using namespace std;
const int N = 3e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
priority_queue,greater >qu;
void solve()
{
int q;
cin>>q;
int y=0;
while(q--)
{
int a,x;
cin>>a;
if(a==1)
{
cin>>x;
qu.push(x-y);
}
else if(a==2)
{
cout<>x;
y+=x;
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
13.stringstream用法:
#include
using namespace std;
const int N = 3e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve() {
string s;
getline(cin, s);
stringstream ss(s);
string word;
while (ss >> word) {
cout << word << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
例题:F - Andy’s First Dictionary UVA - 10815
#include
using namespace std;
const int N = 3e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve() {
string s, word;
setse;
while (cin >> s) {
for (int i = 0; i < s.size(); i++) {
if (isalpha(s[i]))
s[i] = tolower(s[i]);
else
s[i] = ' ';
}
stringstream ss(s);
while (ss >> word) {
se.insert(word);
}
}
for (set::iterator it = se.begin(); it != se.end(); it++) {
cout << *it << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
solve();
return 0;
}
/*
样例输入:
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."
So they went home.
*/
14.lower_bound用法:(二分查找)
#include
using namespace std;
const int N = 3e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve() {
//lower_bound
/*
lower_bound函数是C++标准库中的一个函数,
用于在有序序列中查找第一个大于或等于给定值的元素的位置。
它返回一个迭代器,指向序列中第一个大于或等于给定值的元素。
*/
int a[5] = {1, 2, 3, 4, 5};
int tag1 = 3;
int n1 = lower_bound(a, a + 5, tag1) - a;
cout << n1 << endl;
vectorve = {1, 2, 3, 4, 5};
int tag2 = 3;
int n2 = lower_bound(ve.begin(), ve.end(), tag2) - ve.begin();
cout << n2;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
例题:D - Where is the Marble? UVA - 10474
#include
using namespace std;
const int N = 1e4 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve() {
int n, q, cn = 1;
while (cin >> n >> q && n && q) {
int a[N];
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
printf("CASE# %d:\n", cn);
cn++;
while (q--) {
int x;
cin >> x;
int r = lower_bound(a, a + n, x) - a;
if (a[r] == x)
printf("%d found at %d\n", x, r + 1);
else
printf("%d not found\n", x);
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
/*
样例输入:
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
*/