A 水题:用字符串就很好处理
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
for(int i=0;i<s.length();i++){
if(s[i] == '7'){
cout<<"Yes"<<endl;
return 0;
}
}
cout<<"No"<<endl;
}
B. 水题
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n;
cin>>n;
long long sum = 0;
for(long long i=1;i<=n;i++){
if(i%3==0&&i%5==0){
continue;
}
if(i%3==0){
continue;
}
if(i%5==0){
continue;
}
sum += i;
}
cout<<sum<<endl;
return 0;
}
C 水题:暴力就行
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll k;
cin>>k;
ll sum = 0;
ll res;
for(ll i=1;i<=k;i++){
for(ll j=1;j<=k;j++){
for(ll s=1;s<=k;s++){
res = __gcd(i,j);
res = __gcd(res,s);
//cout<<res<<"--";
sum += res;
}
}
}
//cout<<endl;
cout<<sum<<endl;
return 0;
}
D 思路:暴力肯定会超时,我采用二分,然后不知道怎么W了两个点
#include<bits/stdc++.h>
using namespace std;
int R[4005];
int G[4005];
int B[4005];
int main(){
int l;
cin>>l;
string s;
cin>>s;
int tot = 0;
int res;
int num1=0;
int num2=0;
int num3=0;
for(int i=0;i<l;i++){
if(s[i] == 'R'){
R[num1] = i;
num1++;
}
if(s[i] == 'G'){
G[num2] = i;
num2++;
}
if(s[i] == 'B'){
B[num3] = i;
num3++;
}
}
if(num1==0||num2==0||num3==0) {
cout<<"0"<<endl;
}
else{
int num;
int i,j;
for(int i=0;i<=l-3;i++){
for(int j=i+1;j<=l-2;j++){
if(s[i]=='R'&&s[j]=='G'){
res = (upper_bound(B,B+num3,j) - B);
tot += num3 - res;
if(binary_search(B,B+num3,2*j-i)) tot--;
}
if(s[i]=='G'&&s[j]=='R'){
res = (upper_bound(B,B+num3,j) - B);
tot += num3 - res;
if(binary_search(B,B+num3,2*j-i)) tot--;
}
if(s[i]=='R'&&s[j]=='B'){
res = (upper_bound(G,G+num2,j) -G);
tot += num2 - res;
if(binary_search(G,G+num2,2*j-i)) tot--;
}
if(s[i]=='B'&&s[j]=='R'){
res = (upper_bound(G,G+num2,j) -G);
tot += num2 - res;
if(binary_search(G,G+num2,2*j-i)) tot--;
}
if(s[i]=='G'&&s[j]=='B'){
res = (upper_bound(R,R+num1,j) -R);
tot += num1 - res;
if(binary_search(R,R+num1,2*j-i)) tot--;
}
if(s[i]=='B'&&s[j]=='G'){
res = (upper_bound(R,R+num1,j) -R);
tot += num1 - res;
if(binary_search(R,R+num1,2*j-i)) tot--;
}
}
}
cout<<tot<<endl;
}
return 0;
}
AC代码
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main(void){
int N; cin >> N;
string S; cin >> S;
ll r=0, g=0, b=0;
for (auto c : S){
if (c=='R') r++;
if (c=='G') g++;
if (c=='B') b++;
}
ll ans = r * g * b;
for (int d = 1; d <= N; d++){
for (int m = 1; m < N-1; m++){
if (m-d<0||m+d>N-1) continue;
if (S[m-d]!=S[m+d] && S[m-d]!=S[m] && S[m]!=S[m+d]) ans--;
}
}
cout << ans << endl;
}