题目1: 中国剩余定理 广场上有一队士兵,如果排成10 列纵队,最后剩下a 个人(0 <= a <= 9);如果排成9 列纵队,最后剩下b 个人(0 <= b <= 8);如果排成8 列纵队,最后剩下c 个人(0 <= c <= 7)……如果排成2 列纵队,最后剩下i 个人(0 <= i <= 1),输入a, b, c,…, i,输出广场上士兵的最少可能人数,分别用蛮力法和数学建模方法求解。
4396 -> 6 4 4 0 4 1 0 1 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 package org.hantou.test1;import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class test1 { public static int x, y; public static int violentWay (int [] a) { int result = 1 ; while (true ) { boolean valid = true ; for (int i = 0 ; i < a.length; i++) { int remaining = a[i]; int columns = i + 2 ; if (result % columns != remaining) { valid = false ; break ; } } if (valid) { return result; } result++; } } public static int mathWay (Map<Integer,Integer> a,int [] w) { int M = 210 ; int [] c1 = new int [11 ]; for (Map.Entry<Integer,Integer> entry :a.entrySet()){ c1[entry.getKey()]=M/entry.getKey(); } int [] c2 = new int [11 ]; for (Map.Entry<Integer,Integer> entry :a.entrySet()){ ex_gcd(c1[entry.getKey()],entry.getKey()); c2[entry.getKey()]=((x+entry.getKey())%entry.getKey()); } int ans=0 ; for (Map.Entry<Integer,Integer> entry :a.entrySet()){ ans+=a.get(entry.getKey())*c1[entry.getKey()]*c2[entry.getKey()]%M; } while (!check(w,ans)) ans+=M; return ans; } public static int ex_gcd (int a, int b) { if (b == 0 ) { x = 1 ; y = 0 ; return a; } int d = ex_gcd(b, a%b); int temp = x; x = y; y = temp - a/b * y; return d; } public static boolean check (int a[],int x) { for (int i = 0 ; i < a.length; i++) { if (x%(i+2 )!=a[i]) return false ; } return true ; } public static void main (String[] args) { Scanner scanner = new Scanner (System.in); System.out.println("请输入剩余人数a到i:" ); String input = scanner.nextLine(); String[] soldiersString = input.split(" " ); int [] a = new int [soldiersString.length]; for (int i = 0 ; i < soldiersString.length; i++) { a[soldiersString.length-i-1 ] = Integer.parseInt(soldiersString[i]); } System.out.println(violentWay(a)); Map<Integer,Integer> t = new HashMap <Integer,Integer>(); t.put(2 ,a[0 ]); t.put(3 ,a[1 ]); t.put(5 ,a[3 ]); t.put(7 ,a[5 ]); System.out.println(mathWay(t,a)); } }
题目2: 对角线的之字形方针 给定一个正整数n,输出如下 n ´ n “之字形”方阵。
例: 8 ´ 8 方阵。
选作:输入任意的两个正整数m 和n (m ¹ n ),构造m ´ n “之字形”矩阵,以下示例为3 ´ 5 矩阵:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 import java.util.Scanner;public class test2 { public static int [][] a = new int [1000 ][1000 ]; public static void main (String[] args) { System.out.println("请输入m n;若要求n*n的方阵,请输入n n:" ); Scanner scanner = new Scanner (System.in); String str = scanner.nextLine(); String s[] = str.split(" " ); int m = Integer.parseInt(s[0 ]); int n = Integer.parseInt(s[1 ]); getmatrix(m,n); } private static void getmatrix (int m, int n) { int cnt = 1 ; for (int i = 0 ; i < m + n - 1 ; i++) { if (i % 2 == 0 ) { for (int j = Math.min(i, m - 1 ); j >= 0 ; j--) { int k = i - j; if (k < n) { a[j][k] = cnt++; } } } else { for (int j = Math.min(i, n - 1 ); j >= 0 ; j--) { int k = i - j; if (k < m) { a[k][j] = cnt++; } } } } for (int i = 0 ; i < m; i++) { for (int j = 0 ; j < n; j++) { System.out.print(a[i][j]+" " ); } System.out.println(); } } }
题目3: ab*cde=fijk 用1、2、3、4、5、6、7、8、9这9 个数字,填入 ? 中使等式??*???=????成立,每个数字恰好只用一次。
以下是所有的7 个答案供参考:
12 483 = 5796、18 297 = 5346、27 * 198 = 5346、
28 157 = 4396、39 186 = 7254、42 * 138 = 5796、
48 * 159 = 7632。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class test3 { private static boolean check (int a, int b, int product) { int [] num = new int [10 ]; num[a / 10 ]++; num[a % 10 ]++; num[b / 100 ]++; num[(b / 10 ) % 10 ]++; num[b % 10 ]++; num[product / 1000 ]++; num[(product / 100 ) % 10 ]++; num[(product / 10 ) % 10 ]++; num[product % 10 ]++; for (int i = 1 ; i <= 9 ; i++) { if (num[i] != 1 ) { return false ; } } return true ; } public static void main (String[] args) { for (int a = 12 ; a <= 98 ; a++) { for (int b = 123 ; b <= 987 ; b++) { int product = a * b; if (product > 9999 ) { break ; } if (check(a, b, product)) { System.out.println(a + " * " + b + " = " + product); } } } } }
题目4: 滑动窗口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import java.util.Scanner;public class test4 { public static void main (String[] args) { System.out.println("请输入正整数n:" ); Scanner scanner = new Scanner (System.in); String s = scanner.nextLine(); int n = Integer.parseInt(s); getAns(n); } private static void getAns (int n) { int cnt = 0 ; int st = 1 , ed = 1 ; int sum=1 ; while (st<=n/2 ){ if (sum<n){ sum+=++ed; }else if (sum>n){ sum-=st++; }else { cnt++; for (int i=st;i<ed;i++) System.out.print(i+"+" ); System.out.println(ed); sum-=st++; } } System.out.println("共有" +cnt+"个不同连续自然段" ); } }
题目5: 一个整数n(n <= 30)可以有多种分划,分划的整数之和为n,在不区分分划出各整数的次序时,字典序递减输出n 的各详细分划方案和分划总数。
例如n = 6,程序输出为:
6
5 1
4 2
4 1 1
3 3
3 2 1
3 1 1 1
2 2 2
2 2 1 1
2 1 1 1 1
1 1 1 1 1 1
total = 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import java.util.ArrayList;import java.util.Scanner;public class test5 { public static void main (String[] args) { System.out.println("请输入正整数n:" ); Scanner scanner = new Scanner (System.in); String s = scanner.nextLine(); int n = Integer.parseInt(s); getAns(n); } private static void getAns (int n) { int cnt = 0 ; for (int i=n;i>0 ;i--){ ArrayList<Integer> a = new ArrayList <>(); a.add(i); if (i!=n){ int m=i,g=n; while (g-m>m){ a.add(m); g-=m; } a.add(g-m); } for (int k = 0 ; k < a.size(); k++) { System.out.print(a.get(k)+" " ); } System.out.println(); cnt++; for (int j = a.size()-1 ; j >0 ; j--) { if (a.get(j)>1 ){ a.set(j, a.get(j)-1 ); a.add(1 ); for (int k = 0 ; k < a.size(); k++) { System.out.print(a.get(k)+" " ); } System.out.println(); j++; cnt++; } } } System.out.println("total = " +cnt); } }