【Java笔试强训】day16编程题

目录

  • 编程题
    • 扑克牌大小
    • 完全数计算

编程题

扑克牌大小

【Java笔试强训】day16编程题_第1张图片

import java.util.Scanner;

@SuppressWarnings({"all"})
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] pp = str.split("-");
        String[] p1 = pp[0].split(" ");
        String[] p2 = pp[1].split(" ");
        String p = "34567891JQKA2";

        if (pp[0].equals("joker JOKER") || pp[1].equals("joker JOKER")) {
            System.out.println("joker JOKER");
        } else if (p1.length == p2.length) {
            if (p.indexOf(p1[0].substring(0, 1)) > p.indexOf(p2[0].substring(0, 1))) {
                System.out.println(pp[0]);
            } else {
                System.out.println(pp[1]);
            }
        } else if (p1.length == 4) {
            System.out.println(pp[0]);
        } else if (p2.length == 4){
            System.out.println(pp[1]);
        } else {
            System.out.println("ERROR");
        }
    }
}

完全数计算

【Java笔试强训】day16编程题_第2张图片

import java.util.Scanner;

@SuppressWarnings({"all"})
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int count = 0;
            for (int i = 2; i <= n; i++) {
                int sum = 0;
                for (int j = 2; j <= Math.sqrt(i); j++) {
                    if (i % j == 0) {
                        if (i / j == j) {
                            sum += j;
                        } else {
                            sum = sum + j + i / j;
                        }
                    }
                }
                if (sum + 1 == i) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}

你可能感兴趣的:(【Java笔试强训】48day,java,开发语言,jvm)