福州大学第十届程序设计竞赛 -- 部分题解


题目传送:福州大学第十届程序设计竞赛



Problem A 神庙逃亡


水题


AC代码:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;

int s, h, vx, vy;

int main() {
	int w;
	cin >> w;
	
	while(w--){
		cin >> s >> h >> vx >> vy;
		long long int t = s / vx;
		long long q = vy * t - 5 * t* t;
		
		if( q > h){
			printf("good done!\n");
		}
		else{
			printf("poor Merida!\n");
		}
	}
	return 0;
}





Problem B 又见LKity


可以把匹配串s1全变为大写,原串s3中的也改为大写放到tmp中,匹配了就输出s2,不匹配就输出s3


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;

char s1[105];
char s2[105];
char s3[50005];
char tmp[50005];

int lens1;

int judge(int x) {
	for(int i = 0; i < lens1; i ++) {
		if(tmp[x + i] != s1[i]) return 0;
	}
	return 1;
}

int main() {
	while(gets(s1) != NULL) {
		//getchar();
		lens1 = strlen(s1);
		gets(s2);
		gets(s3);
		int len = strlen(s3);
		for(int i = 0; i < len; i++) {
			if(s3[i] <= 'z' && s3[i] >= 'a') {
				tmp[i] = s3[i] - 32;
			}
			else tmp[i] = s3[i];
		}
		tmp[len] = '\0';
		
		for(int i = 0; s1[i] != '\0'; i++) {
			if(s1[i] <= 'z' && s1[i] >= 'a') s1[i] -= 32;
		}
		//printf("%s %s %s\n", s1, s3, tmp);
		
		for(int i = 0; i < len; i ++) {
			if(tmp[i] == s1[0]) {
				if(judge(i)) {
					i += lens1 - 1;
					printf("%s", s2);
				}
				else putchar(s3[i]);
			}
			else putchar(s3[i]);
		}
		printf("\n");
	}
	return 0;
}






Problem C 数字的孔数


水题


AC代码:

#include <cstdio>
using namespace std;

int a[10] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1};

int main() {
	int T;
	scanf("%d", &T);
	while(T --) {
		int n;
		scanf("%d", &n);
		int ans = 0;
		while(n) {
			ans += a[n % 10];
			n /= 10;
		}
		printf("%d\n", ans);
	}
	return 0;
}






Problem D 吃豆人


队友做的,貌似有点....


AC代码:

#include<cstdio>
#include<deque>
#include<cmath>
#include<cstring>
#include<algorithm>
#define INF 99999999.0
using namespace std;
class Node
{
public:
	int x,y;
	double v;	
};
int n,m;
int sx,sy,ax,ay,bx,by;
int map[25][25];//0障碍,1空地,2加速器 
double dist[25][25];
double dis[25][25];
bool tag[25][25];
char s[50];
void bfs1()
{
	double A,B,C;
	int i,j,l,r;
	deque<Node> Q;
	Node temp,tmp;temp.v=1.0;temp.x=sx;temp.y=sy;
	dist[sx][sy]=0;
	bool flag=0;
	Q.push_back(temp);
	while(!Q.empty())
	{
		temp=Q.at(0);
		Q.pop_front();flag=0;
		if(temp.x==ax)
		{
			if(temp.y<ay)
			{
				l=temp.y;r=ay;
			}
			else
			{
				l=ay;r=temp.y;
			}
			for(i=l;i<=r;i++)
				if(map[temp.x][i]==0)
					flag=1;
			if(flag==0&&dist[ax][ay]>dist[temp.x][temp.y]+abs(temp.y-ay)*2*0.1)
				dist[ax][ay]=dist[temp.x][temp.y]+abs(temp.y-ay)*2*0.1;
		}flag=0;
		if(temp.y==ay)
		{
			if(temp.x<ax)
			{
				l=temp.x;r=ax;
			}
			else
			{
				l=ax;r=temp.x;
			}
			for(i=l;i<=r;i++)
				if(map[i][temp.y]==0)
					flag=1;			
			if(flag==0&&dist[ax][ay]>dist[temp.x][temp.y]+abs(temp.x-ax)*2*0.1)
				dist[ax][ay]=dist[temp.x][temp.y]+abs(temp.x-ax)*2*0.1;			
		}
		if(temp.x-1>0&&dist[temp.x-1][temp.y]>dist[temp.x][temp.y]+temp.v&&map[temp.x-1][temp.y]!=0)
		{
			tmp.x=temp.x-1;tmp.y=temp.y;tmp.v=temp.v;
			dist[tmp.x][tmp.y]=dist[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);	
		}
		if(temp.x+1<=n&&dist[temp.x+1][temp.y]>dist[temp.x][temp.y]+temp.v&&map[temp.x+1][temp.y]!=0)
		{
			tmp.x=temp.x+1;tmp.y=temp.y;tmp.v=temp.v;
			dist[tmp.x][tmp.y]=dist[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);
		}
		if(temp.y-1>0&&dist[temp.x][temp.y-1]>dist[temp.x][temp.y]+temp.v&&map[temp.x][temp.y-1]!=0)
		{
			tmp.x=temp.x;tmp.y=temp.y-1;tmp.v=temp.v;
			dist[tmp.x][tmp.y]=dist[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);	
		}
		if(temp.y+1<=m&&dist[temp.x][temp.y+1]>dist[temp.x][temp.y]+temp.v&&map[temp.x][temp.y+1]!=0)
		{
			tmp.x=temp.x;tmp.y=temp.y+1;tmp.v=temp.v;
			dist[tmp.x][tmp.y]=dist[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);
		}						
	}
}
double bfs2()
{
	deque<Node> Q;
	Node temp,tmp;temp.x=sx;temp.y=sy;temp.v=0.0;
	Q.push_back(temp);tag[sx][sy]=1;
	while(!Q.empty())
	{
		temp=Q.at(0);
		Q.pop_front();
		if(temp.x-1>0&&tag[temp.x-1][temp.y]==0&&map[temp.x-1][temp.y]!=0)
		{
			tmp.x=temp.x-1;tmp.y=temp.y;tmp.v=temp.v+1.0;
			if(tmp.x==bx&&tmp.y==by)
				return tmp.v;
			tag[tmp.x][tmp.y]=1;
			Q.push_back(tmp);	
		}
		if(temp.x+1<=n&&tag[temp.x+1][temp.y]==0&&map[temp.x+1][temp.y]!=0)
		{
			tmp.x=temp.x+1;tmp.y=temp.y;tmp.v=temp.v+1.0;
			if(tmp.x==bx&&tmp.y==by)
				return tmp.v;
			tag[tmp.x][tmp.y]=1;
			Q.push_back(tmp);
		}
		if(temp.y-1>0&&tag[temp.x][temp.y-1]==0&&map[temp.x][temp.y-1]!=0)
		{
			tmp.x=temp.x;tmp.y=temp.y-1;tmp.v=temp.v+1.0;
			if(tmp.x==bx&&tmp.y==by)
				return tmp.v;			
			tag[tmp.x][tmp.y]=1;
			Q.push_back(tmp);	
		}
		if(temp.y+1<=m&&tag[temp.x][temp.y+1]==0&&map[temp.x][temp.y+1]!=0)
		{
			tmp.x=temp.x;tmp.y=temp.y+1;tmp.v=temp.v+1.0;
			if(tmp.x==bx&&tmp.y==by)
				return tmp.v;			
			tag[tmp.x][tmp.y]=1;
			Q.push_back(tmp);
		}					
	}
}
void bfs3()
{
	int i,j,l,r;
	deque<Node> Q;
	Node temp,tmp;temp.v=0.5;temp.x=bx;temp.y=by;
	dis[bx][by]=0;
	bool flag=0;
	Q.push_back(temp);
	while(!Q.empty())
	{
		temp=Q.at(0);
		Q.pop_front();flag=0;
		if(temp.x==ax)
		{
			if(temp.y<ay)
			{
				l=temp.y;r=ay;
			}
			else
			{
				l=ay;r=temp.y;
			}
			for(i=l;i<=r;i++)
				if(map[temp.x][i]==0)
					flag=1;
			if(flag==0&&dis[ax][ay]>dis[temp.x][temp.y]+abs(temp.y-ay)*2*0.1)
				dis[ax][ay]=dis[temp.x][temp.y]+abs(temp.y-ay)*2*0.1;
		}flag=0;
		if(temp.y==ay)
		{
			if(temp.x<ax)
			{
				l=temp.x;r=ax;
			}
			else
			{
				l=ax;r=temp.x;
			}
			for(i=l;i<=r;i++)
				if(map[i][temp.y]==0)
					flag=1;			
			if(flag==0&&dis[ax][ay]>dis[temp.x][temp.y]+abs(temp.x-ax)*2*0.1)
				dis[ax][ay]=dis[temp.x][temp.y]+abs(temp.x-ax)*2*0.1;			
		}
		if(temp.x-1>0&&dis[temp.x-1][temp.y]>dis[temp.x][temp.y]+temp.v&&map[temp.x-1][temp.y]!=0)
		{
			tmp.x=temp.x-1;tmp.y=temp.y;tmp.v=temp.v;
			dis[tmp.x][tmp.y]=dis[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);	
		}
		if(temp.x+1<=n&&dis[temp.x+1][temp.y]>dis[temp.x][temp.y]+temp.v&&map[temp.x+1][temp.y]!=0)
		{
			tmp.x=temp.x+1;tmp.y=temp.y;tmp.v=temp.v;
			dis[tmp.x][tmp.y]=dis[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);
		}
		if(temp.y-1>0&&dis[temp.x][temp.y-1]>dis[temp.x][temp.y]+temp.v&&map[temp.x][temp.y-1]!=0)
		{
			tmp.x=temp.x;tmp.y=temp.y-1;tmp.v=temp.v;
			dis[tmp.x][tmp.y]=dis[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);	
		}
		if(temp.y+1<=m&&dis[temp.x][temp.y+1]>dis[temp.x][temp.y]+temp.v&&map[temp.x][temp.y+1]!=0)
		{
			tmp.x=temp.x;tmp.y=temp.y+1;tmp.v=temp.v;
			dis[tmp.x][tmp.y]=dis[temp.x][temp.y]+temp.v;
			Q.push_back(tmp);
		}						
	}
}
int main()
{
	//freopen("xx.in","r",stdin);
	//freopen("xx.out","w",stdout);
	int i,j;double ans;
	double A,B,C;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		B=INF;
		for(i=1;i<=20;i++)
			for(j=1;j<=20;j++)
			{
				dist[i][j]=INF;
				dis[i][j]=INF;
			}
		memset(tag,0,sizeof(tag));		
		for(i=1;i<=n;i++)
		{
			scanf("%s",s);
			for(j=0;j<m;j++)
			{
				if(s[j]=='P')
				{
					sx=i;sy=j+1;
				}
				if(s[j]=='B')
				{
					ax=i;ay=j+1;
				}
				if(s[j]=='S')
				{
					bx=i;by=j+1;
				}				
				if(s[j]=='X')
					map[i][j+1]=0;
				if(s[j]=='S')
					map[i][j+1]=2;
				if(s[j]=='.'||s[j]=='B'||s[j]=='P')
					map[i][j+1]=1;			
			}
		}
		bfs1();A=dist[ax][ay];
		B=bfs2();
		bfs3();C=dis[ax][ay];
		if(A<B+C)
			ans=A;
		else
			ans=B+C;	
		if(INF-ans<1.000)
			printf("-1\n");
		else
			printf("%.1lf\n",ans);
	}
	return 0;
}




Problem E 简单的等式


其实也很简单,只要明确一点,x肯定小于等于根号n,然后从x = 根号n开始,往下枚举,不过直接枚举也会超时,所以可以发现s(x,m)是有一定范围的,当x一直减小的过程中,s(x, m)是增大的,所以可以适当设置一个度量,超出这个即跳出循环


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

LL T, n, m;
LL ans;

LL fun(LL x, LL m) {
	LL ret = 0;
	while(x) {
		ret += x % m;
		x /= m;
	}
	return ret;
}

int main() {
	scanf("%I64d", &T);
	while(T --) {
		scanf("%I64d %I64d", &n, &m);
		LL x = sqrt(n * 1.0);
		ans = -1;
		while(x) {
			if(n % x == 0) {
				LL sum = fun(x, m);
				if(sum == n / x - x) {
					ans = x;
				}
			}
			if(n / x - x > 100) {
				break;
			}
			x --;
		}
		cout << ans << endl;
	}
	return 0;
}





Problem G 养鸡场


直接枚举a1和a2肯定会超时,所以优化一下,只枚举a1的值,然后根据a3和各种制约推出a2的取值范围,只要知道a2的取值范围,a3也唯一确定


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;

int x1, y1, x2, y2, x3, y3;
int n;

int main() {
	while(scanf("%d", &n) != EOF) {
		scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
		LL ans = 0;
		for(int i = x1; i <= y1; i ++) {
			int L = max(i, max(x2, n - y3 - i) );
			L = max(L, n / 2 - i + 1);
			int R = min((n- i)/2, min(y2, n - x3 - i));
			if(R - L + 1 > 0) ans += (R - L + 1);
			//cout << L << " " << R << endl;
			//cout << ans << endl;
		}
		cout << ans << endl;
	}
	return 0;
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值