PPT题目

发布时间 2023-09-21 21:42:47作者: 牟兆迪
 1 //生成一千个随机数
 2 public class LinearCongruentialGenerator {
 3     private static final long MODULUS = 2147483647; // 2^31 - 1
 4     private static final long MULTIPLIER = 75;
 5     private static final long INCREMENT = 0;
 6     
 7     private long state;
 8 
 9     public LinearCongruentialGenerator(long seed) {
10         this.state = seed;
11     }
12 
13     public int nextRandomInt() {
14         state = (MULTIPLIER * state + INCREMENT) % MODULUS;
15         return (int) state;
16     }
17 
18     public static void main(String[] args) {
19         int numberOfRandomInts = 1000;
20 
21         LinearCongruentialGenerator generator = new LinearCongruentialGenerator(System.currentTimeMillis());
22 
23         for (int i = 0; i < numberOfRandomInts; i++) {
24             int randomInt = generator.nextRandomInt();
25             System.out.println(randomInt);
26         }
27     }
28 }
29 //用递归求一个数的阶乘
30 public class FactorialCalculator {
31 
32     public static long calculateFactorial(int n) {
33         if (n == 0 || n == 1) {
34             return 1;
35         } else {
36             return n * calculateFactorial(n - 1);
37         }
38     }
39 
40     public static void main(String[] args) {
41         int number = 5; // 替换为您要计算阶乘的整数
42         long factorial = calculateFactorial(number);
43         System.out.println(number + "的阶乘是:" + factorial);
44     }
45 }