当前位置:   article > 正文

【2020牛客网笔经整理】小红书笔试题_public static int numzero( inti xx int count = 0;

public static int numzero( inti xx int count = 0; for (inti=1;i
输入一个自然数n,求表达式 f(n) = 1!2!3!..n! 的结果末尾有几个连续的0?
public class Num0 {
    public static int numZero(int n){
        int count=0;
        int num=n/10*2+(n%10>4?2:0);
        for(int i=0;i<num/2;i++){
            count+=n-(4+10*i)+((n-(9+10*i))>0?(n-(9+10*i)):0);
        }

        return count;
    }
    public static int result=0;
    public static int numZero1(int n){
        for(int i=1;i<=n;i++){
           int p=5;

           while (i/p>=1){
               result+=i/p;
               p*=5;
           }
        }
        return result;
    }
    private static void find5(int i){
        while (i%5==0){
            result+=1;
            i=i/5;
        }
    }

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println(numZero1(23));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

最上面的是我一开始的想法,找规律;显然难以应对数以万计的数据量而且思路有点偏,一开始想的是找2和5的对数,没有想到有5就有2;

然后是双重暴力循环,也是可以的就是会超时;

最后就是中间这个函数,判断五的个数,使用的是p*=p;这是我在评论看到的方法;其实还没想太明白;但是是正解;

给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表。k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序。

说明:

  1. 你需要自行定义链表结构,将输入的数据保存到你的链表中;
  2. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换;
  3. 你的算法只能使用常数的额外空间。

翻转链表做过,但是这种还是有点蒙;
链表题我一直很害怕,感觉维护节点很麻烦;
这道题我的困惑:
1、输入时如何将next链在下一个要输入的节点;
2、如何控制循环;

import java.util.Scanner;

class ListNode{
    int val;
    ListNode next;
    public ListNode(int val){
        this.val=val;
    }
}
public class LinkList {
    public static ListNode ReverserList(ListNode root,int k){
        ListNode ptr=root;

        while (ptr!=null){
            for(int i=0;i<k;i++){
                ptr.next.next=ptr;
            }
        }
        return ptr;
    }
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        String s=scanner.nextLine();
        int k=scanner.nextInt();
        String []strings=s.split(" ");

        ListNode head=new ListNode(0);
        ListNode tail=head;

        for(String str:strings){
            ListNode newNode=new ListNode(Integer.valueOf(str));
            tail.next=newNode;
            tail=tail.next;
        }
        head=head.next;

        ListNode node=reverseGroup(head,k);
        while (node!=null){
            System.out.print(node.val+" ");
            node=node.next;
        }

    }

    private static ListNode reverseGroup(ListNode head, int k) {
        if(head==null||head.next==null||k<=1){
            return head;
        }
        ListNode currentNode = head;

        for(int count=1;count<k;count++){
            currentNode=currentNode.next;
            if(currentNode==null){
                return head;
            }
        }
        ListNode next=currentNode.next;

        reverse(head,currentNode);
        head.next=reverseGroup(next,k);
        return currentNode;
    }

    private static void reverse(ListNode head, ListNode tail) {
        if(head==null||head.next==null){
            return;
        }
        ListNode pre=null;
        ListNode next=null;
        while (pre!=tail){
            next=head.next;
            head.next=pre;
            pre=head;
            head=next;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

这个是看了评论后得到的答案;
输入时只需要将字符串变为链表,我还以为动态的改变;
然后答主条理清晰,先是大体上知道反转的链表段;
head.next=reverseGroup(next,k);这个来连接每k个链表,因为之前reserse函数后,head指向的是最后的节点,所以直接连接即可;
然后局部反转,和之前写过的反转链表函数同;

幼儿园一个大班要分成两个小班,有些小朋友不希望自己和其他某几位小朋友同班。园长向大家收集了不希望同班的要求,然后视情况将一个大班的小朋友分成两个班。请你开发一个程序,帮助园长快速判断是否所有小朋友的不同班请求都可以被满足。

哈哈哈哈哈哈,这道题我AC了;

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static int isClass(int n,int m,int[][]quest ){
        int res=1;
        HashSet<Integer> set1=new HashSet<>();
        HashSet<Integer> set2=new HashSet<>();
        set1.add(quest[0][0]);
        set2.add(quest[0][1]);
        for(int i=1;i<m;i++){
            if(set1.contains(quest[i][0])&&!set1.contains(quest[i][1])){
                set2.add(quest[i][1]);
            }else if(set2.contains(quest[i][0])&&!set2.contains(quest[i][1])){
                set1.add(quest[i][1]);
            }else if(!set1.contains(quest[i][0])&&!set2.contains(quest[i][0])){
                set2.add(quest[i][1]);
                set1.add(quest[i][0]);
            }else {
                res=0;
            }
        }
        return res;

    }
    public static void main(String[] args) {
       Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        scanner.nextLine();
        int m=scanner.nextInt();
        scanner.nextLine();
        int [][]quest=new int[m][2];
        for(int i=0;i<m;i++){
            for(int j=0;j<2;j++){
                quest[i][j]=scanner.nextInt();
            }
        }
    
        System.out.println(isClass(n,m,quest));

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

思路就是构建两个set集合看成两个班;
然后将二维数组的第一列和第二列进行分配;
每一行的两列数据需要在两个set中,否则就是错的返回0;

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签