A - Station and Bus
解题方法:这一题只要判断一个字符串中有A和B两种字符串即可
#include<iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll read(){
ll x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9'){
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return w*x;
}
int main(){
string ptr;
cin>>ptr;
int a=0,b=0;
for(int i=0;i<ptr.length();i++){
if(ptr[i]=='A'){
a++;
}
if(ptr[i]=='B'){
b++;
}
}
if(a&&b){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
} B - Count Balls
解题方法:这一题比较简单,要你判断前N个球有几个蓝色的球,只要进行分类讨论即可
#include<iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll read(){
ll x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9'){
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return w*x;
}
int main(){
ll N,A,B;
cin>>N>>A>>B;
if(N==(A+B)){
cout<<A<<endl;
}else if(N>(A+B)){
ll num=A+B;/*总数*/
ll t=N/num;/*可以分成t组,每组有A个蓝的*/
ll e=N%num;/*这是余数*/
ll y=0;
if(e>=A){
y=A;
}else{
y=e;
}
cout<<A*t+y<<endl;
}else if(N<(A+B)){
if(N>=A){
cout<<A<<endl;
}else{
cout<<N<<endl;
}
}
return 0;
} C - Tax Increase
解题方法:这一题也比较简单,但要注意它的不存在的条件,还有就是注意题目要求下取整
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
int ans=-1;
for(int i=1;floor(i*0.08)<=min(a,b);i++){
if(floor(i*0.08)==a&&floor(i*0.1)==b){
ans=i;
break;
}
}
cout<<ans<<endl;
return 0;
} D - String Formation
解题方法:这一题就比较有意思了,刚开始的时候去暴力,结果TLE了,其实这一题始有技巧的,你要统计它总共翻转了多少次,然后根据它的奇偶性来判断,这样就可以避免无谓的翻转,至于添加字母这里,也要根据这一特性去操作,值得注意的是当翻转次数为奇数时,处在后面的字符串也要翻转,当为偶数时,处在前面的字符串要翻转
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int q;
cin>>q;
int t,f;
char c;
string front,end;
front.clear();
end.clear();
int re_num=0;
for(int i=0;i<q;i++){
cin>>t;
if(t==1){
re_num++;
}else if(t==2){
cin>>f;
cin>>c;
if(f==1){
if(re_num%2==0){
front.push_back(c);
}else{
end.push_back(c);
}
}else{
if(re_num%2==0){
end.push_back(c);
}else{
front.push_back(c);
}
}
}
}
if(re_num%2!=0){
reverse(end.begin(), end.end());
}else{
reverse(front.begin(), front.end());
}
string ans;
if(re_num%2!=0){
reverse(s.begin(), s.end());
ans = end + s + front;
}else{
ans = front + s + end;
}
cout<<ans<<endl;
return 0;
} 


京公网安备 11010502036488号