字节跳动笔试题: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;
    }
}

 

转载于:https://www.cnblogs.com/haimishasha/p/11610237.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值