牛客刷题笔记-远景智能2021秋季招聘软件技术笔试题

1、输入任意一个正整数,计算出它的阶乘得数尾部有几个连续的0. 题目编写完毕需要计算出1000的阶乘得数尾部有几个连续的0,需要把这个统计数字打印输出

import java.util.*;
public class Main{
    static int count0=0;
    static int count5=0;
    static int count2=0;
     
    public static int countzero(int num,int count){
        if(num%10!=0){
            if(num%5==0) {
                count5=countfive(num,count5);
            }
            else if(num%2==0) {
                count2=counttwo(num,count2);
            }
            return count;
        }
        else{
            return countzero(num/10,count+1);
        }
    }
    public static int countfive(int num,int count){
        if(num%5!=0){
            return count;
        }
        else{
            return countfive(num/5,count+1);
        }
    }
    public static int counttwo(int num,int count){
        if(num%2!=0){
            return count;
        }
        else{
            return counttwo(num/2,count+1);
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        for(int i=1;i<=num;i++){
            if(i%10==0){
                count0=countzero(i,count0);
            }
            else if(i%5==0){
                count5=countfive(i,count5);
            }
            else if(i%2==0){
                count2=counttwo(i,count2);
            }
        }
        count0+=(count2<count5?count2:count5);
        System.out.println(count0);
        sc.close();
    }
}

2、在英文中,有一些标点符号需要成对使用,达到闭合的效果。例如双引号("") 大括号({}) 方括号([])

现在我们需要检测指定文本中的 双引号,大括号, 方括号是否闭合

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        Stack<Character> stack=new Stack<Character>();
        int flag = 0;
        boolean result = true;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)=='}'){
                if(stack.pop()!='{') {
                	result = false;
                	break;
                }
            }
            if(str.charAt(i)==']'){
            	if(stack.pop()!='[') {
                	result = false;
                	break;
                }
            }
            if(str.charAt(i)=='{'||str.charAt(i)=='['){
                stack.add(str.charAt(i));
            }
            if(str.charAt(i)=='"') {
            	stack.add(str.charAt(i));
            	flag+=1;
            }
        }
        System.out.println(result&&flag%2==0);
        sc.close();
    }
}

3、某风电场每台风机的发电量和距离升压站的距离各不相同,如风机1:发电量30,距离20;风机2:发电量35,距离25;风机3:发电量25,距离18……,要求在输电总距离限定(如小于100)的前提下,选择风机向升压站输电,使得输送的电量最大。

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int max = 0;
        String str = sc.nextLine();
        String[] str1 = str.split(" ");
        int[] dist = new int[str1.length];
        for(int i=0;i<str1.length;i++){
            dist[i] = Integer.parseInt(str1[i],10);
        }
        str = sc.nextLine();
        String[] str2 = str.split(" ");
        int[] elec = new int[str2.length];
        for(int i=0;i<str1.length;i++){
           elec[i] = Integer.parseInt(str2[i],10);
        }
        int sumdist = sc.nextInt();
        for(int j=0;j<elec.length;j++){
            if(max<elec[j]) max=elec[j];
            for(int k=j+1;k<elec.length;k++){
                if( dist[j] + dist[k]>sumdist) break;
                if(max < elec[j] + elec[k]) max = elec[j] + elec[k];
            }
        }
        System.out.println(max);
        sc.close();
    }
}

4、给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数。

返回的结果必须要是按升序排好的。如果有两个数与 x 的差值一样,优先选择数值较小的那个数。

import java.util.*;
public class Main{
    private static void QuickSort(int[] num, int left, int right) {
        if(left>=right) {
            return;
        }
        int key=num[left];
        int i=left;
        int j=right;
        while(i<j){
            while(num[j]>=key && i<j){
                j--;
            }
            while(num[i]<=key && i<j){
                i++;
            }
            if(i<j){
                int temp=num[i];
                num[i]=num[j];
                num[j]=temp;
            }
        }
        num[left]=num[i];
        num[i]=key;
        QuickSort(num,left,i-1);
        QuickSort(num,i+1,right);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] str1 = str.split(",");
        int[][] arr = new int[str1.length][2];
        for(int i=0;i<str1.length;i++){
            arr[i][0] = Integer.parseInt(str1[i],10);
        }
        int k = sc.nextInt();
        int x = sc.nextInt();
        int[] num = new int[str1.length];
        for(int i=0;i<str1.length;i++){
            arr[i][1] = Math.abs(arr[i][0]-x);
            num[i] = Math.abs(arr[i][0]-x);
        }
        QuickSort(num,0,num.length-1);
        int max = num[k-1];
        int[] result = new int[k];
        for(int i=0;i<k;i++){
            if(arr[i][1]<=max) result[i]=arr[i][0];
        }
        for(int i=0;i<k-1;i++) {
            System.out.print(result[i]+",");
        }
        System.out.println(result[k-1]);
        sc.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值