A
#include<bits/stdc++.h>
using namespace std;
#define int long long
int f(int x)
{
int sum=0;
while(x)
{
sum+=x%10;
x/=10;
}
return sum;
}
signed main()
{
int t;
cin>>t;
while(t--)
{
int x;
cin>>x;
int k=f(x);
cout<<k<<endl;
}
}
B
用pair插入4种状态,循环暴力模拟
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
int t, a1, a2, b1, b2;
vector<PII> player1, player2;
int p1_win, p2_win;
int res;
int main()
{
cin >> t;
while (t -- )
{
cin >> a1 >> a2 >> b1 >> b2;
player1 = {{a1, a2}, {a2, a1}};
player2 = {{b1, b2}, {b2, b1}};
res = 0;
for (auto &p1 : player1)
{
for (auto &p2 : player2)
{
p1_win = 0, p2_win = 0;
if (p1.first > p2.first)
{
p1_win ++ ;
}
else if (p1.first < p2.first)
{
p2_win ++ ;
}
if (p1.second > p2.second)
{
p1_win ++ ;
}
else if (p1.second < p2.second)
{
p2_win ++ ;
}
if (p1_win > p2_win)
{
res ++ ;
}
}
}
cout << res << '\n';
}
return 0;
}
C
只需间隔区间长度大于s,注意要插入首端与末尾的区间
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>PII;
int n,m,s;
signed main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>s>>m;
vector<PII>v;
v.push_back({0,0});
for(int i=1;i<=n;i++)
{
int l,r;
cin>>l>>r;
v.push_back({l,r});
}
v.push_back({m,m});
bool st=0;
for(int i=0;i<v.size()-1;i++)
{
if(v[i+1].first-v[i].second>=s)
st=1;
}
if(st)
cout<<"YES";
else cout<<"NO";
cout<<endl;
}
}
D
按照贪心的原则,对于字符串中的?尽量满足与子字符串中字符匹配,其他字符也尽量满足匹配.每次匹配成功l向后移动一位当l越界后重新变为0,k记录长度当大于子字符串长度则满足题意。
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int T;
cin>>T;
while(T--)
{
string s1,s2,s3="";
cin>>s1>>s2;
int res=0,l=0,k=0;
for(int i=0;i<s1.size();i++)
{
if(s1[i]==s2[k])
{
l++;
k++;
s3+=s1[i];
}
else if(s1[i]=='?')
{
s3+=s2[k];
l++;
k++;
}
else s3+=s1[i];
if(k>=s2.size())
k=0;
}
if(l>=s2.size())
{
cout<<"YES"<<endl;
cout<<s3;
}
else cout<<"NO";
cout<<endl;
}
}
E
思路:
1. 若要将所有数字变为0,则只能操作[y/3] ,则要多进行操作[y/3 ] ,另一个数字要进行x * 3,则x = 0
2. 什么数字最容易变为0 , 最小的数字,先让最小的数字变为0,然后让其他的数字变为0
3. 每个数字变为0的操作次数是不变的,则可以用前缀和
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int f[N]; int a[N];
int s(int num)
{
int count = 0;
while(num)
{
count++; num /= 3;
}
return count;
}
void solve()
{
int l , r;
cin>>l>>r;
cout << f[r] - f[l-1] + a[l] << endl;
}
int main()
{
int T;
cin>>T;
f[0] = 0;
for(int i = 1; i <= 2e5;i++)
{
a[i]=s(i);
f[i]=f[i-1] + a[i];
}
while(T--)
{
solve();
}
return 0;
}