网易游戏雷火盘古题

声明:题都来自牛客网,如果侵权,请联系我,我马上删除

字符串编码
给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){
	string s1;
	getline(cin,s1);
	int len=s1.length();
	int j,sum,i;
	for(i=0;i<len;){
		j=1;
		sum=1;
		while(i+j<len && s1[i]==s1[i+j]){
			sum++;
			j++;
		}
		printf("%d",sum);
		printf("%c",s1[i]);
		i+=j;
	}
	printf("\n");
	return 0;
}



最大和
在一个N*N的数组中寻找所有横,竖,左上到右下,右上到左下,四种方向的直线连续D个数字的和里面最大的值

#include <iostream>

using namespace std;

int main(){
	int n,d,max;
	int a[102][102];
	int a1[102][102];
	int a2[102][102];
	int a3[102][102];
	int a4[102][102];
	scanf("%d%d",&n,&d);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int x;
			scanf("%d",&x);
			a[i][j]=x;
			a1[i][j]=x;
			a2[i][j]=x;
			a3[i][j]=x;
			a4[i][j]=x;
		}
	}
	max=0;
	//printf("横\n");
	for(int i=0;i<n;i++){
		for(int j=1;j<n;j++){
			
			if(j<d-1){
				a1[i][j]+=a1[i][j-1];
			}
			else {
				if(j==d-1){
					a1[i][j]+=a1[i][j-1];
				}
				else{
					a1[i][j]=a1[i][j]+a1[i][j-1]-a[i][j-d];
				}
				if(a1[i][j]>max)
					max=a1[i][j];
			}
		}
	}
	//printf("竖\n");
	for(int i=1;i<n;i++){
		for(int j=0;j<n;j++){
			if(i<d-1){
				a2[i][j]+=a2[i-1][j];
			}
			else{
				if(i==d-1){
					a2[i][j]+=a2[i-1][j];
				}
				else{
					a2[i][j]=a2[i][j]+a2[i-1][j]-a[i-d][j];
				}
				if(a2[i][j]>max)
					max=a2[i][j];
			}
		}
	}
	//printf("正斜\n");
	for(int i=1;i<n;i++){
		for(int j=1;j<n;j++){
			if(j<d-1 || i<d-1){
				a3[i][j]+=a3[i-1][j-1];
			}
			else{
				if(j==d-1 && i==d-1){
					a3[i][j]+=a3[i-1][j-1];
				}
				else{
					a3[i][j]=a3[i][j]+a3[i-1][j-1]-a[i-d][j-d];
				}
				if(a3[i][j]>max)
					max=a3[i][j];
			}
		}
	}
	//printf("反斜\n");
	for(int i=n-2;i>=0;i--){
		for(int j=1;j<n;j++){
			if(j<d-1 || (n-i-1)<d-1){
				a4[i][j]+=a4[i+1][j-1];
			}
			else{
				if(j==d-1 && (n-i-1)==d-1){
					a4[i][j]+=a4[i+1][j-1];
				}
				else{
					a4[i][j]=a4[i][j]+a4[i+1][j-1]-a[i+d][j-d];
				}
				if(a4[i][j]>max)
					max=a4[i][j];
			}
			
		}
	}
	//printf("over\n");
	printf("%d\n",max);
	return 0;
}



推箱子
大家一定玩过“推箱子”这个经典的游戏。具体规则就是在一个N*M的地图上,有1个玩家、1个箱子、1个目的地以及若干障碍,其余是空地。玩家可以往上下左右4个方向移动,但是不能移动出地图或者移动到障碍里去。如果往这个方向移动推到了箱子,箱子也会按这个方向移动一格,当然,箱子也不能被推出地图或推到障碍里。当箱子被推到目的地以后,游戏目标达成。现在告诉你游戏开始是初始的地图布局,请你求出玩家最少需要移动多少步才能够将游戏目标达成。

这是大神的,我的总有一个错误不知道为何
#include <iostream>
#include <string>
#include <queue>
#include <vector>
 
using namespace std;
struct humanbox{
        int hx,hy,bx,by;
        humanbox(int x,int y,int bbx,int bby):hx(x),hy(y),bx(bbx),by(bby){};
};
int main()
    {
    int n,m;
    cin>>n>>m;  
    vector<vector<int>> map(n,vector<int>(m,0));
    int hx,hy,bx,by;
    int endx,endy;
    for(int i=0;i!=n;++i)
        {
        string str;
        cin>>str;
        for(int j=0;j!=m;++j)
            {
            if(str[j]=='X')
                {
                map[i][j]='X';
                hx=i;
                hy=j;
            }
            else if(str[j]=='#')
                map[i][j]='#';               
            else if(str[j]=='@')
                {
                map[i][j]='@'; 
                endx=i;
                endy=j;
            }
            else if(str[j]=='*')
                {
                map[i][j]='*';
                bx=i;
                by=j;
            }
        }
    }
    int stepx[4]={0,0,1,-1};
    int stepy[4]={1,-1,0,0};
    int count[10][10][10][10]={0};
    queue<humanbox> que;
    que.push(humanbox(hx,hy,bx,by));
    count[hx][hy][bx][by]=1;
    while(!que.empty())
        {
        humanbox top_que=que.front();
        que.pop();
        if(top_que.bx==endx&&top_que.by==endy)
            {
            cout<<(count[top_que.hx][top_que.hy][top_que.bx][top_que.by])-1<<endl;
            return 0;
        }
        for(int i=0;i!=4;++i)
            {
            int hnx=top_que.hx+stepx[i];
            int hny=top_que.hy+stepy[i];
            if(hnx<0||hny<0||hnx>=n||hny>=m||map[hnx][hny]=='#') continue;
 
            if(hnx==top_que.bx&&hny==top_que.by)
                {
                int bnx=top_que.bx+stepx[i];
                int bny=top_que.by+stepy[i];
                if(bnx<0||bny<0||bnx>=n||bny>=m||map[bnx][bny]=='#') continue;
                if(count[hnx][hny][bnx][bny]) continue;
                count[hnx][hny][bnx][bny]=count[top_que.hx][top_que.hy][top_que.bx][top_que.by]+1;
                que.push(humanbox(hnx,hny,bnx,bny));                      
            }
 
            else
                {
                if(count[hnx][hny][top_que.bx][top_que.by])
                    continue;
                count[hnx][hny][top_que.bx][top_que.by]=count[top_que.hx][top_que.hy][top_que.bx][top_que.by]+1;
                que.push(humanbox(hnx,hny,top_que.bx,top_que.by));               
            }
 
        }
 
    }
 
    cout<<-1<<endl;
    return 0;
 
}



赛马
在一条无限长的跑道上,有N匹马在不同的位置上出发开始赛马。当开始赛马比赛后,所有的马开始以自己的速度一直匀速前进。每匹马的速度都不一样,且全部是同样的均匀随机分布。在比赛中当某匹马追上了前面的某匹马时,被追上的马就出局。 请问按以上的规则比赛无限长的时间后,赛道上剩余的马匹数量的数学期望是多少

#include <iostream> 
    
using namespace std; 
    
int main(){ 
    int n; 
    scanf("%d", &n); 
    double ans = 0; 
    for (int i = 1; i <= n; ++i) { 
        ans += 1.0 / i; 
    } 
    printf("%.4f\n", ans); 
    return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值