CSP第一次模拟赛

A - 咕咕东的奇遇

问题描述

将26个英文字母首尾相连,从a处开始选择,按照给定字符串的顺序将字符串中的字母一一在这个首尾相连的序列中找到,计算出所需要的最短步数。

Input

输入只有一行,是一个字符串。

Output

输出最少要转的次数。

Example
Input

zeus

Output

18

思路

直接打表算出每个字母到每个字母的个数,然后存在26*26的二维数组中,for循环走一遍,答案直接加进去就可以了。

代码
#include<iostream>
#include<cstring>

using namespace std;

int main()
{
	int a[30][30];
	for (int i=0;i<14;i++)
		a[0][i]=i;
	for (int i=14;i<26;i++)
		a[0][i]=26-i;
	for (int i=1;i<26;i++)
	{
		a[i][0]=a[i-1][25];
		for (int j=1;j<26;j++)
			a[i][j]=a[i-1][j-1];
	}
	
	string s;
	int l,ans;
	int c[10010];
	ans=0;
	cin>>s;
	l=s.length();
	c[0]=0;
	for (int i=1;i<=l;i++)
		c[i]=s[i-1]-'a';
	for (int i=0;i<l;i++)
		ans=ans+a[c[i]][c[i+1]];
	cout<<ans<<endl;
	
	return 0;
} 

B - 咕咕东想吃饭

问题描述

生煎店两种购买方式:①在某一天一次性买两个生煎。②今天买一个生煎,同时为明天买一个生煎,店家会给一个券,第二天用券来拿,没有其余的购买方式(可以无限次购买)。给定一个长度为n的数组,严格按照上述两种方式购买生煎,问是否能够每天食用a[i]个生煎且n天后无卷剩余。

注意:卷第二天必须使用,不能存起来。

Input

输入两行,第一行输入一个正整数n(1<=n<=100000)(1<=n<=100000),表示考试总天数。

第二行有n个数,第i个数a[i](0<=a[i]<=10000)
表示第i天咕咕东要买的生煎的数量。

Output

能够每天食用a[i]个生煎且n天后无卷剩余,输出"YES",如果不能满足,输出“NO”。(输出不带引号)

Example
Input1

4
1 2 1 2

Output1

YES

Input2

3
1 0 1

Output2

NO

思路

从前往后按天数处理,对于第一天,双数则直接买两个吃完,单数则最后需要买一张券,即吃掉明天的一个,所以a[i]单数我们直接a[i+1]-1,这样若a[i+1]>0则说明可以继续处理,若<0则要输出NO,直到最后一天结束后,检查一下a[n+1]是否为-1就行了。

代码
#include<iostream>

using namespace std;

int a[100010];

int main()
{
	int n;
	cin>>n;
	for (int i=0;i<n;i++)
		cin>>a[i];
	a[n]=0;
	for (int i=0;i<n;i++)
		if (a[i]>=0)
		{
			if (a[i]%2==1)
				a[i+1]=a[i+1]-1;
		}
		else 
		{
			cout<<"NO"<<endl;
			return 0;
		}
	if (a[n]==-1) cout<<"NO"<<endl;
	else cout<<"YES"<<endl;
	
	return 0;
}

C-可怕的宇宙射线

问题描述

宇宙射线会在无限的二维平面上传播(可以看做一个二维网格图),初始方向默认向上。 宇宙射线会在发射出一段距离后分裂, 向该方向的左右45°方向分别分裂出两条宇宙射线,同时威力不变。宇宙射线会分裂n次,每次分裂后会在分裂方向前进a[i]个单位长度。求出分裂n次后宇宙射线的覆盖的方格数。

在这里插入图片描述

Input

输入第一行包含一个正整数n(n <= 30),表示宇宙射线会分裂n次
第二行包含n个正整数a1, a2, … , 第i个数a[i];(a[i]<= 5)表示第次分裂的宇宙射线会在它原方向上继续走多少个单位长度。

Output

输出一个整数ans,表示宇宙射线的覆盖范围。

Example
Input

4
4 2 2 3

Output

39

思路

此题没有AC。
我的思路是因为每次最多只能走5,最多走30次,所以300*300的二维空间已经足够了,所以我每次直接扫一遍二维空间,判断当前次有哪几个点要往哪几个方向走,然后将这几个点走出去,并且将终点存入下一次要走的数组中,走过的点会直接存在foot[][]中,初始foot为false,只要有点走过,foot为true,最后统计true个数,就是答案。

代码
#include<iostream>

using namespace std;

struct point
{
	int t;								//t表示需要走几个方向 
	int d[10];							//d表示具体的方向 
};

point dir[350][350];						//此轮中该点是否为起点,不为-1表示是起点,且方向就是数字 
point newDir[350][350];
bool foot[350][350];					//该点是否被走过 
int dx[8]={0,1,1,1,0,-1,-1,-1};				//方向为01234567,分别为北、东北、东、东南、南、西南、西、西北
int dy[8]={1,1,0,-1,-1,-1,0,1};

int main()
{
	int n,x,y,len,d1,d2;
	cin>>n;
	
	for (int i=0;i<350;i++)
		for (int j=0;j<350;j++)
		{
			dir[i][j].t=0;
			newDir[i][j].t=0;
			foot[i][j]=false;
		}
	n--;
	cin>>len;
	x=151;y=151;
	for (int l=0;l<len;l++)
	{
		x=x+dx[0];
		y=y+dy[0];
		foot[x][y]=true;
	}
	dir[x][y].t=1;
	dir[x][y].d[0]=1;
	
/*	for (int i=141;i<171;i++)
	{
		for (int j=141;j<171;j++)
			if (foot[i][j])
				cout<<"1 ";
			else cout<<"  ";
		cout<<endl;
	}*/
	while (n>0)
	{
		cin>>len;
		for (int i=0;i<302;i++)
			for (int j=0;j<302;j++)
				if (dir[i][j].t!=0)
				{
					x=i;
					y=j;
					for (int f=0;f<8;f++)
						if (dir[i][j].d[f]==1)
						{			
							d1=(f+7)%8;
							for (int l=0;l<len;l++)
							{
								x=x+dx[d1];
								y=y+dy[d1];
								foot[x][y]=true;
							}
							newDir[x][y].t=1;
							newDir[x][y].d[d1]=1;
							
							x=i;
							y=j;
							d2=(f+9)%8;
							for (int l=0;l<len;l++)
							{
								x=x+dx[d2];
								y=y+dy[d2];
								foot[x][y]=true;
							}
							newDir[x][y].t=1;
							newDir[x][y].d[d2]=1;
						}
				//	cout<<i<<" "<<j<<endl;
				}
		for (int i=0;i<302;i++)
			for (int j=0;j<302;j++)
			{
				dir[i][j]=newDir[i][j];
				for (int f=0;f<8;f++)
					newDir[i][j].d[f]=0;
				newDir[i][j].t=0;
			}
		n--;
		
/*		for (int i=141;i<171;i++)
		{
			for (int j=141;j<171;j++)
				if (foot[i][j])
					cout<<"1 ";
				else cout<<"  ";
			cout<<endl;
		}*/
		
	}
	
/*	for (int i=141;i<171;i++)
	{
		for (int j=141;j<171;j++)
			if (foot[i][j])
				cout<<"1 ";
			else cout<<"  ";
		cout<<endl;
	}*/
	
	int ans=0;
	for (int i=0;i<302;i++)
		for (int j=0;j<302;j++)
			if (foot[i][j])
			{
//				cout<<i-151<<" "<<j-151<<endl;
				ans++;
			}
	cout<<ans<<endl;
	
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值