从if-else到格与布尔代数:复杂条件、权限与SQL化简的工程视角
2026/11/3 4:37:48
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int q = in.nextInt(); while (q-- != 0) { int m = in.nextInt(), x = in.nextInt(); System.out.println((x - 1) % m + 1); } } }运行结果
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ long n = Integer.valueOf(sc.nextLine()); for(int i =0;i<n;i++){ String str = sc.nextLine(); long num = Long.parseLong(str); int total = 0; int length = str.length(); for(int j =0;j<length;j++){ int value = str.charAt(j)-48; if(value==0){ total++; }else if(num%value==0)total++; } outPut(total,length); } } } public static void outPut(int total,int length){ if(total==length){ System.out.println("G"); }else if(total>0){ System.out.println("H"); }else{ System.out.println("S"); } } }运行结果
import java.util.Scanner; public class Main { public static int find(int num) { int lst_val = 1; for (int i = 1; i <= num; i++) { int tmp = lst_val * i; if (tmp % 10 == 0) while (tmp % 10 == 0) tmp = tmp / 10; lst_val = tmp % 10; //保留最后两个非0数。连long整型都不用 tmp /= 10; lst_val = lst_val + tmp % 10 * 10; } return lst_val % 10; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int num = sc.nextInt(); int res = find(num); System.out.println(res); } } }运行结果
import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long n = scanner.nextLong(); scanner.close(); List<Long> factors = primeFactorization(n); // 输出结果 for (int i = 0; i < factors.size(); i++) { if (i > 0) { System.out.print(" "); } System.out.print(factors.get(i)); } } /** * 对n进行质因数分解 * @param n 要分解的数 * @return 质因数列表,按从小到大排序 */ private static List<Long> primeFactorization(long n) { List<Long> factors = new ArrayList<>(); // 处理2的情况(唯一的偶质数) while (n % 2 == 0) { factors.add(2L); n /= 2; } // 处理奇数的情况,从3开始 for (long i = 3; i * i <= n; i += 2) { while (n % i == 0) { factors.add(i); n /= i; } } // 如果剩余的n本身是一个质数(大于2) if (n > 1) { factors.add(n); } return factors; } }运行结果
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n-- > 0) { int x = sc.nextInt(); int count = 0; // 质因数分解:从最小质数2开始检查 // 循环条件i*i <= x:减少不必要的检查,优化效率 for (int i = 2; i * i <= x; i++) { // 若i是x的质因数,统计其出现次数 while (x % i == 0) { x = x / i; // 除去所有当前质因数i count++; } } // 若剩余x大于1,说明x本身是一个质因数 if (x > 1) { count++; } // 根据质因数总个数的奇偶性输出结果 if (count % 2 == 1) { System.out.println("kou"); } else { System.out.println("yukari"); } } sc.close(); } }运行结果