程序星编程之路

A.Love “A”

img

思路:

题意很明确:希望字符串有一半是’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.谁拿了最多奖学金

img

思路:

这里使用结构体,里面存学生姓名name,期末分数final, 班级评议成绩other,判断是否是学生干部f1(f1=1表示是,否则f1=0),判断是否是西部省份学生(f2=1表示是,否则f2=0),,发表的论文数量sci,获得的奖学金money.然后就是5个奖项的判断条件:

  1. 院士奖学金:final>80&&sci>=1,得到8000元
  2. 五四奖学金:t[i].final>85&&t[i].other>80,得到4000元
  3. 成绩优秀奖:t[i].final>90,得到2000元
  4. 西部贡献奖:t[i].final>85&&t[i].f2,得到1000元
  5. 班级贡献奖: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.玩具谜题

img

思路:

结构体记录人的状态(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Phoenix_ZengHao

创作不易,能否打赏一瓶饮料?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值