想面试先笔试应该是目前大部分IT岗位的不可或缺的一环了,本人之前投递的简历有幸通过初筛,获得了笔试的机会,笔试分为选择题和编程题,采用双机位模式,编程题看着眼熟,可惜发挥不太好!!!下面给大家分享一下两道遇到的编程题吧!
今天遇到机考的编程题是两道之前备考蓝桥杯遇到的类似的提问,难度和填空题类似,以下是题目思路
一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数,也称为素数。
规定:0和1既不是质数,也不是合数,最小的质数为2
首先,质数是一个大于1的自然数,所以如果输入的数小于等于1,直接可以判定它不是质数。
其次,2是最小的质数,因此如果输入的数等于2,可以直接判定它是质数。
如果输入的数是一个偶数(除2外),则可以直接判定它不是质数,因为偶数除了2之外都能被2整除。
对于大于2的奇数,我们需要遍历从3开始到该数的平方根的所有奇数,检查是否有整数因子。如果存在整数因子,就可以判定它不是质数。如果没有找到整数因子,就可以判定它是质数。
最后,如果上述条件都不满足,那么就可以判定输入的数是质数。
要求:输入3,返回3是质数,方法名为Main,需要自己手写要导什么包
-
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("请输入一个自然数:");
- int userNumber = scanner.nextInt();
- scanner.close();
-
- if (isPrime(userNumber)) {
- System.out.println(userNumber + " 是质数");
- } else {
- System.out.println(userNumber + " 不是质数");
- }
- }
-
- public static boolean isPrime(int number) {
- if (number <= 1) {
- return false;
- } else if (number == 2) {
- return true;
- } else if (number % 2 == 0) {
- return false;
- } else {
- for (int i = 3; i <= Math.sqrt(number); i += 2) {
- if (number % i == 0) {
- return false;
- }
- }
- return true;
- }
- }
- }
-
- public class PrimeNumberChecker {
- public static boolean isPrime(int number) {
- // 负数和0、1都不是质数
- if (number <= 1) {
- return false;
- }
-
- // 循环检查可能的因子,从2到待判断数的平方根
- for (int i = 2; i <= Math.sqrt(number); i++) {
- if (number % i == 0) {
- // 找到能整除的因子,不是质数
- return false;
- }
- }
-
- // 如果没有找到能整除的因子,是质数
- return true;
- }
-
- public static void main(String[] args) {
- int number = 17; // 要判断的数字
- if (isPrime(number)) {
- System.out.println(number + " 是质数");
- } else {
- System.out.println(number + " 不是质数");
- }
- }
- }
- public class PrimeNumberChecker {
- public static boolean isPrime(int number) {
- // 负数和0、1都不是质数
- if (number <= 1) {
- return false;
- }
-
- // 2是唯一的偶数质数
- if (number == 2) {
- return true;
- }
-
- // 如果待判断的数为偶数,那么不是质数
- if (number % 2 == 0) {
- return false;
- }
-
- // 从3开始,逐个检查可能的因子,只检查奇数因子
- for (int i = 3; i <= Math.sqrt(number); i += 2) {
- if (number % i == 0) {
- // 找到能整除的因子,不是质数
- return false;
- }
- }
-
- // 如果没有找到能整除的因子,是质数
- return true;
- }
-
- public static void main(String[] args) {
- int number = 17; // 要判断的数字
- if (isPrime(number)) {
- System.out.println(number + " 是质数");
- } else {
- System.out.println(number + " 不是质数");
- }
- }
- }
这是第一种,能实现的,可以不符合题目的输出要求;
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("请输入年份:");
- int year = scanner.nextInt();
- System.out.print("请输入月份:");
- int month = scanner.nextInt();
- System.out.print("请输入日期:");
- int day = scanner.nextInt();
- scanner.close();
-
- int dayOfYear = calculateDayOfYear(year, month, day);
- System.out.println("这一天是这一年的第 " + dayOfYear + " 天");
- }
-
- public static int calculateDayOfYear(int year, int month, int day) {
- int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int dayOfYear = day;
-
- for (int i = 1; i < month; i++) {
- dayOfYear += daysInMonth[i];
- }
-
- // 处理闰年的2月
- if (isLeapYear(year) && month > 2) {
- dayOfYear++;
- }
-
- return dayOfYear;
- }
-
- public static boolean isLeapYear(int year) {
- return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- }
- }

例如:2023,8,7,然后程序将按照您的要求进行计算。注意,是用“,”,替代了Enter键,不需要输出中文,方法名为Main,需要自己手写要导什么包
这是按照考试要求来的,可惜当时没时间了,现在做出了
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("请输入年份,月份,日期(使用逗号分隔):");
- String input = scanner.nextLine();
- scanner.close();
-
- String[] parts = input.split(",");
-
- if (parts.length != 3) {
- System.out.println("输入格式不正确,请使用逗号分隔年份、月份和日期。");
- return;
- }
-
- int year = Integer.parseInt(parts[0]);
- int month = Integer.parseInt(parts[1]);
- int day = Integer.parseInt(parts[2]);
-
- int dayOfYear = calculateDayOfYear(year, month, day);
- System.out.println("这一天是这一年的第 " + dayOfYear + " 天");
- }
-
- public static int calculateDayOfYear(int year, int month, int day) {
- int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int dayOfYear = day;
-
- for (int i = 1; i < month; i++) {
- dayOfYear += daysInMonth[i];
- }
-
- // 处理闰年的2月
- if (isLeapYear(year) && month > 2) {
- dayOfYear++;
- }
-
- return dayOfYear;
- }
-
- public static boolean isLeapYear(int year) {
- return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- }
- }

另外的方法,当初属实忘记了(如果你准备参加蓝桥杯这个方法你就很熟悉了),就是使用Java的java.util.Calendar类或java.time.LocalDate类来完成
- import java.util.Scanner;
-
- public class DayOfYearCalculator {
- public static int getDayOfYear(int year, int month, int day) {
- int[] daysPerMonth = {
- 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
- };
-
- int dayOfYear = day;
- for (int i = 1; i < month; i++) {
- dayOfYear += daysPerMonth[i];
- }
-
- // 处理闰年的情况
- if (isLeapYear(year) && month > 2) {
- dayOfYear += 1;
- }
-
- return dayOfYear;
- }
-
- public static boolean isLeapYear(int year) {
- return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- }
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
-
- // 设置逗号为分隔符
- scanner.useDelimiter(",");
-
- System.out.print("请输入日期(格式:年,月,日):");
-
- // 读取用户输入的整行内容
- String input = scanner.nextLine();
-
- // 使用逗号分割输入字符串
- String[] parts = input.split(",");
-
- if (parts.length != 3) {
- System.out.println("输入格式错误,请使用逗号分隔年、月、日。");
- } else {
- int year = Integer.parseInt(parts[0].trim());
- int month = Integer.parseInt(parts[1].trim());
- int day = Integer.parseInt(parts[2].trim());
-
- int dayOfYear = getDayOfYear(year, month, day);
- System.out.println("输入的日期是一年中的第 " + dayOfYear + " 天");
- }
-
- scanner.close(); // 关闭 Scanner
- }
- }
