Java機の試験問題

6812 ワード

整理:自https://cloud.tencent.com/developer/article/1237641
1、回文数
public static boolean palindrom(Integer integer) {
        String str1 = String.valueOf(integer);
        String str2 = new StringBuilder(str1).reverse().toString();
        return str1.equals(str2);
}

public static void main(String[] args) {
        System.out.println(palindrom(1232));
}

2、英字、スペース、数字、その他の文字の個数をそれぞれ集計する1行の文字を入力します.
方法1:正則置換
public class Test {
    public static void main(String[] args) {
        String str = new String("asd123asd");
        System.out.println(abcNum(str));
    }

    public static int abcNum(String str) {
        return str.length()-str.replaceAll("[a-zA-Z]","").length();
    }
}

方法2:正規検査
public class Test {
    public static void main(String[] args) {
        String str = new String("asd123asd");
        System.out.println(abcNum(str));
    }

    public static int abcNum(String str) {
        int count = 0;
        String regex = "[a-zA-Z]";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            count++;
        }
        return count;
    }
}
public static int numNum(String s) {
        return s.length() - s.replaceAll("\\d", "").length();
}
public static int spaceNum(String s) {
        return s.length() - s.replaceAll(" ", "").length();
}
public static int otherNum(String s) {
        return s.length() - s.replaceAll("[^a-zA-Z\\d ]", "").length();
}

3、ある年、ある月、ある日を入力して、この日がこの年の何日目かを判断します.
public static int dayOfYearByDateFormat(int year, int month, int day) {
        SimpleDateFormat sdf = new SimpleDateFormat("DDD");
        return Integer.parseInt(sdf.format(new Date(year, month - 1, day)));
    }
public static int dayOfYearByCalendar(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        return calendar.get(Calendar.DAY_OF_YEAR);
}

public static int dayOfYearByLocalDate(int year, int month, int day) {
        LocalDate date = LocalDate.of(year, month, day);
        return date.getDayOfYear();
    }

4、配列ソート
  :
public static Integer[] sortByArrays(Integer... arr) {
        Arrays.sort(arr,(x, y) -> y - x);
        return arr;
}

  
public static int[] sortByArrays(int... arr) {
        Arrays.sort(arr);
        return arr;
}

  :
public static Integer[] resversedInteger(Integer[] arr) {
        List list = Arrays.asList(arr);
        Collections.reverse(list);
        return list.toArray(new Integer[arr.length]);
}

  :
public static int[] resversedArray(int[] arr) {
        int[] ints = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ints[i] = arr[arr.length - i - 1];
        }
        return ints;
}

public static int[] sort(int... arr) {
        return IntStream.of(arr).sorted().toArray();
}

public static int[] reverseSort(int... arr) {
        return IntStream.of(arr).boxed()
                .sorted(Comparator.reverseOrder())
                .mapToInt(e -> (int) e)
                .toArray();
}

5、逆順に数字を出力し、桁数を統計する
public static String[] d(int num) {
        String s = new StringBuilder(String.valueOf(num)).reverse().toString();
        return new String[]{String.valueOf(s.length()), s};
}

6、数字切り取り指定位置
public static int intSub(int num, int start, int end) {
        String s = String.valueOf(num);
        String substring = s.substring(s.length() - end, s.length() - start + 1);
        return Integer.parseInt(substring);
    }

7、統計文字列におけるサブストリングの出現回数
public static int strNum(String str, String s) {
        int count = 0;
        int i;
        while ((i = str.indexOf(s)) >= 0) {
            str = str.substring(i + s.length());
            count++;
        }
        return count;
}

8、配列を入力し、   最大は最初の要素と交換し、最小は最後の要素と交換します.
public static Double[] startMaxEndMin(Double[] arr) {
        Stream stream = Arrays.stream(arr);
        Double max = stream.max(Double::compareTo).get();
        Double min = stream.min(Double::compareTo).get();
        int i = Arrays.asList(arr).indexOf(max);
        arr[i] = arr[0];
        arr[0] = max;
        //         
        int j = Arrays.asList(arr).indexOf(min);
        arr[j] = arr[arr.length - 1];
        arr[arr.length - 1] = max;
        return arr;
    }

9、素数判断
public static boolean testIsPrime3(int n) {
        if (n <= 3) {
            return n > 1;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

10、1つの偶数はいつも2つの素数の和として表すことができる
public static int[] obb2Prime(int obb) {
        if (obb % 2 != 0) return null;
        for (int i = 2; i < obb / 2; i++) {
            if (isPrime(i) && isPrime(obb - i)) {
                return new int[]{i, obb - i};
            }
        }
        return null;
    }

11、正の整数を素因数に分解する.例えば、90を入力し、90=2*3*3*5を印刷します.
public static List factor(int num) {
        List list = new ArrayList<>();
        for (int i = 2; i <= num + 1; i++) {
            while (num % i == 0 && num != i) {
                list.add(i);
                num /= i;
            }
            if (num == i) {
                list.add(i);
                break;
            }
        }
        return list;
    }

12、公約数公倍数
   
public static int maxCommonDivisor(int a, int b) {
        int temp = a;
        if (a < b) {
            a = b;
            b = temp;
        }
        while (temp != 0) {
            temp = a % b;
            a = b;
            b = temp;
        }
        return a;
    }

   
public static int minCommonMultiple(int a, int b) {
        return a * b / maxCommonDivisor(a, b);
    }

13、完数:一つの数がちょうどその因子の和に等しい場合
public static boolean isWholeNumber(int num) {
        int sum = 0;
        for (int i = 1; i <= num / 2; i++) {
            if (num % i == 0) sum += i;
        }
        return sum == num;
    }

14、水仙の花の数:各位の数字の立方とその数自身の3桁に等しい
public static boolean isNarcissisticNum(int num) {
        int a = num / 100;
        int b = (num / 10) % 10;
        int c = num % 10;
        int sum = a * a * a + b * b * b + c * c * c;
        return sum == num;
    }