zoukankan      html  css  js  c++  java
  • 字节跳动笔试题:1. 小于N的质数数量;2. 逆时针二维数组;3. 判断a+b>c

    1. 小于N的质数数量

    import java.util.Scanner;
     
    /**
     * 计算小于N的质数数量
     * @author Turing
     *
     */
    public class Main4 {
        public static void main( String[] args ) {
            Scanner sc = new Scanner(System.in);
            int [] arr = new int[100];
            int num = 0;
            while(sc.hasNextLine()){
                String str = sc.nextLine().trim();
                if(str.equals("")){
                    break;
                }else{
                    arr[num++] = Integer.valueOf(str);
                }
            }
            for (int i = 0; i < num; i++) {
                System.out.println(primesNum(arr[i]));
            }
        }
     
        public static int primesNum(int n){
            boolean[] num = new boolean[n];
            int number = 0;
            for (int i = 2; i < n; i++) {
                if(!num[i]){
                    number++;
                    for (int j = 2; i*j < n; j++) {
                        num[i*j]=true;
                    }
                }
            }
            return number;
        }
    }

    2. 逆时针二维数组 60%

    import java.util.Scanner;
     
    /**
     * 逆时针打印矩阵
     * @author Turing
     *
     */
    public class Main3 {
        public static void main( String[] args ) {
            Scanner sc = new Scanner(System.in);
            String[] results = new String[100];
            int index = 0;
            while(sc.hasNextLine()){
                String str = sc.nextLine().trim();
                if(str.equals("")){
                    break;
                }else{
                    String[] strs = str.split(" ");
                    int M = Integer.valueOf(strs[0]);
                    int N = Integer.valueOf(strs[1]);
                    int [][] matrix = new int [M][N];
                    int value = 1;
                    for (int i = 0; i < M; i++) {
                        for (int j = 0; j < N; j++) {
                            matrix[i][j] = value;
                            value++;
                        }
                    }
                    results[index++] = spiral(matrix, M, N);
                }
            }
            for (int i = 0; i < index; i++) {
                System.out.println(results[i]);
            }
            
        }
     
        public static String spiral(int[][] matrix,int M,int N){
            String result = "";
            if(M!=0){
                int row1 = 0;
                int row2 = M -1;
                int col1 = 0;
                int col2 = N-1;
                while(row1<=row2 && col1<=col2){
                    for (int i = col2; i >=col1; i--) {
                        result += matrix[row1][i] +" ";
                    }
                    for (int i = row1+1; i <=row2; i++) {
                        result += matrix[i][col1] +" ";
                    }
                    if(row1<row2 && col1<col2){
                        for (int i = col1+1; i < col2; i++) {
                            result += matrix[row2][i] +" ";
                        }
                        for (int i = row2; i > row1; i--) {
                            result += matrix[i][col2] +" ";
                        }
                    }
                    row1++;
                    row2--;
                    col1++;
                    col2--;
                }
            }
            return result.trim();
        }
    }

    3. 判断a+b>c

    import java.util.Scanner;
     
    /**
     * a + b > C
     * int64 int64 int64
     * [-2^36,-2^63-1]
     * @author Turing
     *
     */
    public class Main2 {
        public static void main( String[] args ) {
            Scanner sc = new Scanner(System.in);
            boolean[] results = new boolean[100];
            int index = 0;
            while(sc.hasNextLine()){
                String str = sc.nextLine().trim();
                if(str.equals("")){
                    break;
                }else{
                    String[] strs = str.split(" ");
                    long a = Long.valueOf(strs[0]);
                    long b = Long.valueOf(strs[1]);
                    long c = Long.valueOf(strs[2]);
                    results[index++] = abc(a, b, c);
                }
            }
            for (int i = 0; i < index; i++) {
                System.out.println(results[i]);
            }
        }
     
        public static boolean abc(long a, long b, long c){
            if(a>0 && b>0 && a+b<0){
                return true;
            }
            if(a<0 && b<0 && a+b>0){
                return false;
            }
            return a+b>c?true:false;
        }
    }
  • 相关阅读:
    PowerDesigner导出word表结构
    Java
    Java
    Java
    Java
    Java
    Java
    Java
    Java
    Java
  • 原文地址:https://www.cnblogs.com/haimishasha/p/11610237.html
Copyright © 2011-2022 走看看