A.Love “A”
思路:
题意很明确:希望字符串有一半是’a’,显然我们没有必要删除’a’,只需要删除非’a’的字母,因此统计’a’的数量即可,同时也统计一下不是’a’的字母的数量,把多余的不是’a’的字母去掉即可。答案显然就是a的数量min(n,cnta+cnta-1)
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000+50;
int n;char s[maxn];
int main(){
cin>>s;
n=strlen(s);
int cnta=0;
for(int i=0;i<n;i++)
if(s[i]=='a')
cnta++;
cout<<min(n,cnta+cnta-1)<<endl;
return 0;
}
B.谁拿了最多奖学金
思路:
这里使用结构体,里面存学生姓名name,期末分数final, 班级评议成绩other,判断是否是学生干部f1(f1=1表示是,否则f1=0),判断是否是西部省份学生(f2=1表示是,否则f2=0),,发表的论文数量sci,获得的奖学金money.然后就是5个奖项的判断条件:
- 院士奖学金:final>80&&sci>=1,得到8000元
- 五四奖学金:t[i].final>85&&t[i].other>80,得到4000元
- 成绩优秀奖:t[i].final>90,得到2000元
- 西部贡献奖:t[i].final>85&&t[i].f2,得到1000元
- 班级贡献奖:t[i].other>80&&t[i].f1,得到850元。
ans记录所有人的总奖学金,maxx记录最高的奖学金。然后从头开始再枚举一下看谁是第一个得到最高奖学金的输出他的姓名。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100+50;
struct Node{
char name[25];int final,other,f1,f2,sci,money;
}t[maxn];
int n;
int main(){
scanf("%d",&n);
int maxx=0,ans=0;
for(int i=1;i<=n;i++){
char x,y;
scanf("%s%d%d %c %c%d",&t[i].name,&t[i].final,&t[i].other,&x,&y,&t[i].sci);//注意scanf和cin的区别,如果scanf读入不好,不妨设一下cin
if(x=='Y')t[i].f1=1;
else t[i].f1=0;
if(y=='Y')t[i].f2=1;
else t[i].f2=0;
if(t[i].final>80&&t[i].sci>=1)t[i].money+=8000;
if(t[i].final>85&&t[i].other>80)t[i].money+=4000;
if(t[i].final>90)t[i].money+=2000;
if(t[i].final>85&&t[i].f2)t[i].money+=1000;
if(t[i].other>80&&t[i].f1)t[i].money+=850;
maxx=max(maxx,t[i].money);
ans+=t[i].money;
}
for(int i=1;i<=n;i++){
if(t[i].money==maxx){
printf("%s\n",t[i].name);
break;
}
}
printf("%d\n%d\n",maxx,ans);
return 0 ;
}
C.玩具谜题
思路:
结构体记录人的状态(sta=0表示向内,sta=1表示向外)以及姓名。有n个人,从0~n-1记录所有人的状态和姓名。有m次操作,每次操作有a和s,a表示顺逆,s表示距离步数。当人朝内,a=0,表示顺时针,对应的人的编号-s,a=1,表示逆时针,对应的人编号+s,注意当编号<0,则编号要加+n(因为这是一个环,和取模类似);当编号>=n,则编号要-n。当人朝外,a=0,表示逆时针,对应的人的编号+=s,a=1,表示顺时针,对应的人的编号-=s。朝外的操作和朝内刚好相反(可画图辅助思考)
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+50;
struct Node{
char name[30];int sta;
}t[maxn];
int n,m;
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<=n-1;i++){
scanf("%d%s",&t[i].sta,t[i].name);
}
int now=0;
while(m--){
int s,a;
cin>>a>>s;
if(t[now].sta==0){
//朝内
if(a==0)now-=s;
else now+=s;
if(now<0)now+=n;
if(now>=n)now-=n;
}
else{
if(a==0)now+=s;
else now-=s;
if(now<0)now+=n;
if(now>=n)now-=n;
}
}
printf("%s",t[now].name);
return 0;
}