網易遊戲雷火盤古題

聲明:題都來自牛客網,如果侵權,請聯繫我,我馬上刪除

字符串編碼
給定一個字符串,請你將字符串重新編碼,將連續的字符替換成“連續出現的個數+字符”。比如字符串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; 
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章