2020阿里实习春招笔试

2020阿里实习春招第二批第二道

足球队组队

输入一个数n,代表有n个人,将n个人组成不同人数的小队,每个小队选一个人作为队长,输出有多少种组合,数值太大则mod(10^9+7).
比如n=3,有(加粗为队长)
1
2
3
1 2
1 2
1 3
1 3
2 3
2 3
1 2 3
1 2 3
1 2 3
结果为12(3*2^(3-1)).

输入
3
输出
12

代码

import java.io.IOException;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    static int minlength=Integer.MAX_VALUE;
    public static void main(String[] args) throws IOException {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        System.out.println(new BigInteger(String.valueOf(n)).multiply(new BigInteger("2").pow(n-1)).mod(new BigInteger("1000000007")));
    }
}

小明走迷宫

小明从起点走到终点,求最短路径,S为起点,E为终点,#为障碍。
迷宫有n行,m列。坐标为(1…n,1…m)。
小明有个可以使用5次的传送器,耗费单个时间可从(x,y)传送到(n+1-x,m+1-y)上,前提是传送的目标点没有障碍。
如下所示,最短路径为4。

输入
4 4
#S…
E#…
#…

输出
4

代码

import java.io.IOException;
import java.util.Scanner;

public class Main {
    static int minlength=Integer.MAX_VALUE;
    public static void main(String[] args) throws IOException {
        Scanner in=new Scanner(System.in);
        String[] tmpnm=in.nextLine().trim().split(" ");
        int n=Integer.parseInt(tmpnm[0]);
        int m=Integer.parseInt(tmpnm[1]);
        minlength=n*m;
        int[][] sted=new int[2][2];
        char[][] map=new char[n+1][m+1];
        for(int i=1;i<=n;i++){
            String tmp=in.nextLine().trim();
            for(int j=1;j<=m;j++){
                map[i][j]=tmp.charAt(j-1);
                if(map[i][j]=='S'){
                    sted[0][0]=i;
                    sted[0][1]=j;
                }
                if(map[i][j]=='E'){
                    sted[1][0]=i;
                    sted[1][1]=j;
                }
            }
        }
        //显然,此问题可以采取dfs的方法进行解决,到达终点则返回,比较多个路径的步数,得到最小值即为答案
        dfs(map,sted[0][0],sted[0][1],sted[1],0,0);
        System.out.println(minlength);
    }

    private static void dfs(char[][] map, int x, int y, int[] ed,int len,int count) {
        if(x==ed[0]&&y==ed[1]) minlength=Math.min(minlength,len);
        if(len>=minlength) return;
        if(moveleft(map,x,y)){
            dfs(map,x-1,y,ed,len+1,count);
        }
        if(moveright(map,x,y)){
            dfs(map,x+1,y,ed,len+1,count);
        }
        if(movetop(map,x,y)){
            dfs(map,x,y+1,ed,len+1,count);
        }
        if(movedown(map,x,y)){
            dfs(map,x,y-1,ed,len+1,count);
        }
        if(movemove(map,x,y)&&count<5){
            dfs(map,map.length-x,map[0].length-y,ed,len+1,count+1);
        }
    }
    private static boolean moveleft(char[][] map, int x, int y){
        if(x>=2&&map[x-1][y]!='#') return true;
        return false;
    }
    private static boolean moveright(char[][] map, int x, int y){
        if(x<=map.length-2&&map[x+1][y]!='#') return true;
        return false;
    }
    private static boolean movetop(char[][] map, int x, int y){
        if(y<=map.length-2&&map[x][y+1]!='#') return true;
        return false;
    }
    private static boolean movedown(char[][] map, int x, int y){
        if(y>=2&&map[x][y-1]!='#') return true;
        return false;
    }
    private static boolean movemove(char[][] map, int x, int y){
        if(map[map.length-x][map[0].length-y]!='#') return true;
        return false;
    }
}

一直花了七十多分钟才做完,理所应当的在考试的时候没有AC,枯了,记录一下吧。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值