网易有道面试题集锦

本文汇总了网易有道的面试题目,包括构建n x n矩阵、表达式求值、找零问题、马赛跑策略及寻找字符串的回文子串等算法挑战,同时也涉及哈夫曼树高度的计算问题。
1,打印如下形式的矩阵;
n=5:
1       2       9       10      25
4       3       8       11      24
5       6       7       12      23
16      15      14      13      22
17      18      19      20      21
n=6:
1       2       9       10      25      26
4       3       8       11      24      27
5       6       7       12      23      28
16      15      14      13      22      29
17      18      19      20      21      30

36      35      34      33      32      31

代码:

#include <iostream>
using namespace std;

int main(){
    const int nLen=5;
    int l=2,x=0,y=0;
    int di=0;
    int dx[]={0,1,0,1,0,-1};
    int dy[]={1,0,-1,0,1,0};
    int dirX=dx[0];
    int dirY=dy[0];
    int count=1;
    int input;
    int map[nLen][nLen]={0};
    while(true){
        map[x][y]=count;
        x=x+dirX;
        y=y+dirY;
        if(x>=l||x<0||y>=l||y<0){
            x=x>=l?l-1:x;
            x=x<0?0:x;
            y=y>=l?l-1:y;
            y=y<0?0:y;
            di=(di+1)%6;
            dirX=dx[di];
            dirY=dy[di];
            x=x+dirX;
            y=y+dirY;
            if(count>=l*l){
                l=l+1;
                if(l>nLen)
                    break;
            }
        }
        count++;
    }
	//map[x][y]=count;
    for(int i=0;i<nLen;i++){
        for(int j=0;j<nLen;j++){
            cout<<map[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


2,写一个函数,打印一个如下的n x n的矩阵,例如:
n = 5
1 1 1 1 1
1 2 3 2 1
1 3 6 3 1
1 2 3 2 1
1 1 1 1 1
n = 6
1 1 1 1 1 1
1 2 3 3 2 1
1 3 6 6 3 1
1 3 6 6 3 1
1 2 3 3 2 1
1 1 1 1 1 1

代码:

#include <iostream>
using namespace std;

int main(){
	const int n=5;
	int map[n][n];
	int x,y;
	if(n%2==0){
		for(x=0;x<n/2;x++){
			for(y=0;y<n/2;y++){
				if(x==0||y==0)
					map[x][y]=1;
				else
					map[x][y]=map[x-1][y]+map[x][y-1];
			}
		}
		for(x=n/2;x<n;x++){
			for(y=0;y<n/2;y++){
				map[x][y]=map[n-x-1][y];
			}
		}
		for(x=0;x<n;x++){
			for(y=n/2;y<n;y++){
				map[x][y]=map[x][n-y-1];
			}
		}
	}else{
		for(x=0;x<=n/2;x++){
			for(y=0;y<=n/2;y++){
				if(x==0||y==0)
					map[x][y]=1;
				else
					map[x][y]=map[x-1][y]+map[x][y-1];
			}
		}
		for(x=n/2+1;x<n;x++){
			for(y=0;y<=n/2;y++){
				map[x][y]=map[n-x-1][y];
			}
		}
		for(x=0;x<n;x++){
			for(y=n/2+1;y<n;y++){
				map[x][y]=map[x][n-y-1];
			}
		}
	}
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout<<map[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


3,表达式求值,一个字符串只由'+','-',和‘0’-‘9’组成,并且'+','-'只作为二元运算符。

代码:

#include <iostream>
using namespace std;

int main(){
    char str[200];
    long sum=0;
    long cur_int=0;
    while(true){
        cin>>str;
        if(strcmp(str,"#")==0)
            break;
        int nInt=0;
        int n=strlen(str);
        int cur_opt=1;
        int pre_opt=1;
        sum=0;
        for(int i=0;i<=n;i++){
            if(str[i]=='-'){
                pre_opt=cur_opt;
                cur_opt=-1;
                sum=sum+pre_opt*cur_int;
                cur_int=0;
            }else if(str[i]=='+'){
                pre_opt=cur_opt;
                cur_opt=1;
                sum=sum+pre_opt*cur_int;
                cur_int=0;
            }else if(i==n){
                sum=sum+cur_opt*cur_int;
                cur_int=0;
                break;
            }else{
                cur_int=cur_int*10+str[i]-'0';
            }
        }
        cout<<"res="<<sum<<endl;
    }
    return 0;
}


4,有一个人站在电影院门口卖票,票价50,一开始手上没有找零的钱,现在有两种人来买票,A拿着100元的钱,人数为m(m<20),B拿着50元的钱,人数为n(n<20)。卖票的人必须用从B类人中那里得来钱找给A,所以卖票的顺序是有限制的。

5.36匹马赛跑,跑道同时只能容许6匹马。而且36匹马速度不同,但是每次跑的速度恒定。问,跑多少次可以选出第一,第二,第三名?

6,给定一个字串X,求它最长字串S,使得S=SR,SR为S的反序,即如果S=abc,则SR=cba,例子:X=abccba,则输出S=abc

7,给定n个整数,求对应的哈夫曼树的高度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值