校招编程题集锦

1.题目描述 设有n个正整数,将他们连接成一排,组成一个最大的多位整数。

如:n=3时,3个整数13,312,343,连成的最大整数为34331213。

如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。

输入描述: 有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),

第二行包含N个数(每个数不超过1000,空格分开)。

输出描述: 每组数据输出一个表示最大的整数。

package reed.meituan;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 * Created by fanqunsong on 2017/8/19.
 */
public class Shuchuan {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            ArrayList<Integer> al = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                al.add(sc.nextInt());
            }
                Collections.sort(al, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    String s1 = o1.toString();
                    String s2 = String.valueOf(o2);
                    return (s2+s1).compareTo(s1+s2);
                }
            });
            for(int i = 0; i < n-1; i++){
                System.out.print(al.get(i));
            }
            System.out.println(al.get(n-1));

        }

    }

}

Java String.compareTo()方法用法实例教程, 此方法如果这个字符串是等参数字符串那么​返回值0,

如果这个字符串是按字典顺序小于字符串参数那么返回小于0的值,如果此字符串是按字典顺序大于字符串参数那么一个大于0的值

--------------------------------------------------------

2.给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。

比如: (1) “hello xiao mi”-> “mi xiao hello”

package reed.meituan;

import java.util.Scanner;

/**
 * Created by fanqunsong on 2017/8/19.
 */
public class Juzifanzhuan {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            String[] ss = str.split(" ");
            int len = ss.length;
            for (int i = len-1; i>0; i--){
                System.out.print(ss[i]+" ");
            }
            System.out.println(ss[0]);
        }

    }
}

-------------------------------------------------------

给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。

输入描述: 输入数据一个字符串,包括字母,数字等。 输出描述: 输出首先出现三次的那个英文字符

package reed.meituan;

import java.util.Scanner;

/**
 * Created by fanqunsong on 2017/8/19.
 */
public class Tongjizifu {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){

            String str = sc.nextLine();
            int[] count = new int[128];
            for(int i = 0; i < str.length(); i++){
                if((str.charAt(i) >= 'a'&&str.charAt(i) <= 'z')||(str.charAt(i) >= 'A'&&str.charAt(i) <= 'Z'))
                count[str.charAt(i)]++;
                if(count[str.charAt(i)] > 2){
                    System.out.println(str.charAt(i));
                    break;
                }

            }

        }

    }

}

---------------------------------------------------------

Please create a function to extract the filename extension from the given path,return the extracted filename extension or null if none. 输入描述: 输入数据为一个文件路径 输出描述: 对于每个测试实例,要求输出对应的filename extension

输入

Abc/file.txt

输出

txt

package reed.meituan;

import java.util.Scanner;

/**
 * Created by fanqunsong on 2017/8/20.
 */
public class Filename {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            int lastIndexOf = str.lastIndexOf(".");
            if(lastIndexOf == -1){
                System.out.println("null");
            }else{
                System.out.println(str.substring(lastIndexOf+1));
            }

        }
    }
}

-----------------------------------------------------------

题目描述 有一个X*Y的网格,小团要在此网格上从左上角到右下角,只能走格点且只能向右或向下走。

请设计一个算法,计算小团有多少种走法。给定两个正整数int x,int y,请返回小团的走法数目。

输入描述: 输入包括一行,逗号隔开的两个正整数x和y,取值范围[1,10]。 输出描述: 输出包括一行,为走法的数目

。 示例1 输入 3 2 输出 10

public class Wanggezoufashumu {
       public static void main(String[] args) {
    	       Scanner sc = new Scanner(System.in);
    	       while(sc.hasNext()){
    	    	   int m = sc.nextInt();
    	    	   int n = sc.nextInt();
    	    	   int [][] dp = new int [m+1][n+1];
    	    	   for(int i = 0;i<m+1;i++){
    	    		   dp[i][0]=1;   
    	    	   }
    	    	   for(int i = 0;i < n+1;i++){
    	    		   dp [0][i]=1;
    	    	   }
    	    	   for(int i =1;i<m+1;i++)
    	    		   for(int j =1;j <n+1;j++){
    	    			   dp[i][j]=dp[i-1][j]+dp[i][j-1];
    	    		   }
    	    	   System.out.println(dp[m][n]);
    	       }
	}
}


---------------------------------------------------------------------------

一个数组有 N 个元素,求连续子数组的最大和。 例如:[-1,2,1],和最大的连续子数组为[2,1],

其和为 3 输入描述: 输入为两行。 第一行一个整数n(1 <= n <= 100000),表示一共有n个元素 第二行为n个数,

即每个元素,每个整数都在32位int范围内。以空格分隔。 输出描述: 所有连续子数组中和最大的值。

示例1 输入 3

-1 2 1 输出 3

// 经典dp问题
// 假设dp[n]表示以n为最后一个元素的子数组和的最大值,
// 因此, dp[n] = max(dp[n-1],0)+num[n];
// 当然实现的时候,没有必要设置一个数组保存所有的情况,因为只是用到了前一个位置的计算结果。
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = 0;
        while(sc.hasNext()){
            n = sc.nextInt();
            int[] num = new int[n];
            for(int i=0;i<n;i++){
                num[i] = sc.nextInt();
            }
            int max = num[0];
            int sum = num[0];
            for(int i=1;i<n;i++){
                if(sum>=0){
                    sum += num[i];
                }else{
                    sum=num[i];
                }
                if(sum>max)max=sum;
            }
            System.out.println(max);
        }
    }
}

-------------------------------------------------------------------------------

题目描述 如果一个数字序列逆置之后跟原序列是一样的就称这样的数字序列为回文序列。例如: {1, 2, 1}, {15, 78, 78, 15} , {112} 是回文序列, {1, 2, 2}, {15, 78, 87, 51} ,{112, 2, 11} 不是回文序列。 现在给出一个数字序列,允许使用一种转换操作: 选择任意两个相邻的数,然后从序列移除这两个数,并用这两个数字的和插入到这两个数之前的位置(只插入一个和)。 现在对于所给序列要求出最少需要多少次操作可以将其变成回文序列。 输入描述: 输入为两行,第一行为序列长度n ( 1 ≤ n ≤ 50) 第二行为序列中的n个整数item[i] (1 ≤ iteam[i] ≤ 1000),以空格分隔。 输出描述: 输出一个数,表示最少需要的转换次数 示例1 输入 4 1 1 1 3 输出 2

import java.util.Scanner;
public class Main201 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int times = 0;
            int[]  inputArr = new int[n];
            for (int i = 0; i < n; i++) {
                inputArr[i] = scanner.nextInt();
            }
           int head = 0;
            int tail = n-1;
            while (head<tail){
                if(inputArr[head] == inputArr[tail]){
                    head++;
                    tail--;
                }else{
                    if(inputArr[head] < inputArr[tail]){
                        inputArr[++head] += inputArr[head-1];
                        times ++;
                    }
                    else {
                        inputArr[--tail] += inputArr[tail+1];
                        times++;
                    }
                }
            }
            System.out.println(times);
        }
    }
}

-----------------------------------------------------------------------------------

小易有一个圆心在坐标原点的圆,小易知道圆的半径的平方。小易认为在圆上的点而且横纵坐标都是整数的点是优雅的,小易现在想寻找一个算法计算出优雅的点的个数,请你来帮帮他。 例如:半径的平方如果为25 优雅的点就有:(+/-3, +/-4), (+/-4, +/-3), (0, +/-5) (+/-5, 0),一共12个点。

import java.util.Scanner;

import static java.lang.Math.sqrt;

/*created by fanqunsong
    Date : 2017/11/13
    Time : 20:37
    */
public class Main202 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int times = 0;
            for (int i = 0; i < sqrt(n); i++) {
                double j = sqrt(n - i*i);
                if((int) j >= j){
                    times++;
                }
            }
            System.out.println(times*4);
        }
    }
}


----------------------------------------------------------------------------------

题目描述 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000。

如,输入为10, 程序应该输出结果为2。(共有两对质数的和为10,分别为(5,5),(3,7)) 输入描述: 输入包括一个整数n,(3 ≤ n < 1000) 输出描述: 输出对数 示例1 输入 10 输出 2

import java.util.Scanner;
import static java.lang.Math.sqrt;
public class Main303 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int res = 0;
            int n = scanner.nextInt();
            for (int i = 2; i <  n/2; i++) {
                if(isSS(i)&isSS(n-i)){
                    res++;
                }
            }
            System.out.println(res);
        }
    }
    private static  boolean isSS(int num){
        boolean isSS = true;
        for (int i = 2; i <= sqrt(num); i++) {
            if(num%i == 0){
                isSS = false;
                return isSS;
            }
        }
        return isSS;
    }
}


--------------------------------------------------------------------------------------

给定一个有n个正整数的数组A和一个整数sum,求选择数组A中部分数字和为sum的方案数。 当两种选取方案有一个数字的下标不一样,

我们就认为是不同的组成方案。 输入描述: 输入为两行: 第一行为两个正整数n(1 ≤ n ≤ 1000),sum(1 ≤ sum ≤ 1000) 第二行为n个正整数A[i](32位整数),

以空格隔开。 输出描述: 输出所求的方案数 示例1 输入 5 15 5 5 10 2 3 输出 4

import java.util.Scanner;
public class Main214 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int sum =scanner.nextInt();
            int[] A = new int[n];
            for (int i = 0; i < n; i++) {
                A[i] = scanner.nextInt();

            }
            System.out.println(dp(n,sum,A));
        }
    }
    private static long dp(int n,int sum,int[] A){
        long[] dp = new long[sum+1];
        dp[0] = 1;
        for (int i = 0; i < n; i++) {
            for(int j =sum;j>=A[i];j--){
                dp[j] = dp[j-A[i]]+dp[j];
            }
        }
        return dp[sum];
    }
}

----------------------------------------------------------------------------------------------

题目描述 给你六种面额1、5、10、20、50、100元的纸币,假设每种币值的数量都足够多,

编写程序求组成N员(N为0-10000的非负整数)的不同组合的个数。 输入描述: 输入为一个数字N,即需要拼凑的面额 输出描述: 输出也是一个数字,

为组成N的组合个数。 示例1 输入 5 输出 2

import java.util.Scanner;

/*
1.dp概念:简单来说就是将原来的问题分解成多个子问题,然后将子问题一个一个的解决,最终问题的规模变小了;
2.本题可以使用dp的思想来做,合成一个面值为n的组合数,可以看成合成n-1,n-5,n-10,n-20,n-50,n-100五个面值的组合数之和,然后将问题细分化,
最终可以求出结果,其中我们知道,面值为1的组合数为1
 */
public class Main315 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int N = scanner.nextInt();
            System.out.println(count(N));
        }
    }
    private static long count(int N){
        int[] a = {1,5,10,20,50,100};
        long[] dp = new long[N+1];
        dp[0] = 1;
        for (int i = 0; i < 6; i++) {
            for (int j = 1; j <= N ; j++) {
                if(j>=a[i]){
                    dp[j] = dp[j]+dp[j-a[i]];
                }
            }
        }
        return dp[N];
    }
}


-------------------------------------------------------------------------------------------

Fibonacci数列是这样定义的: F[0] = 0 F[1] = 1 for each i ≥ 2: F[i] = F[i-1] + F[i-2] 因此,Fibonacci数列就形如:0, 1, 1, 2, 3, 5, 8, 13, ...,

在Fibonacci数列中的数我们称为Fibonacci数。给你一个N,你想让其变为一个Fibonacci数,每一步你可以把当前数字X变为X-1或者X+1,

现在给你一个数N求最少需要多少步可以变为Fibonacci数。 输入描述: 输入为一个正整数N(1 ≤ N ≤ 1,000,000) 输出描述:

输出一个最小的步数变为Fibonacci数" 示例1 输入 15 输出 2

import java.util.Scanner;

public class Main117 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int N = sc.nextInt();
            System.out.println(Fib(N));
        }
    }
    private static int Fib(int N){
        int a = 0;
        int b = 1;
        int tmp = 0;
        while (b<=N){
             tmp = b+a;
             a = b;
             b = tmp;
        }
        return (b-N)<(N-a)?(b-N):(N-a);
    }
}

----------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值