课堂测试
#include <iostream> #include <random> #include <time.h> using namespace std; double f1(double a) { return a / 30; } int main() { clock_t start, finish; // 挂钟时间 double t = 0; int f = 0; random_device rd; // 用于获得随机种子 mt19937 gen1(rd()); // 以随机种子初始化随机数生成器 mt19937 gen2(rd()); mt19937 gen3(rd()); uniform_int_distribution<int> dist1(10, 99); // 生成 [10, 99] 范围内的整数 uniform_int_distribution<int> dist2(1, 4); // 生成 [1, 4] 范围内的整数 uniform_real_distribution<double> dist3(10.0000000, 99.00000000); uniform_real_distribution<double> dist4(1.0000000, 4.000000); int a[30] = { 0 }; for (int i = 0; i < 30; i++) { int randomNum1, randomNum2, randomNum3, n; do { randomNum1 = dist1(gen1); // 生成随机数 randomNum2 = dist1(gen2); randomNum3 = dist2(gen3); } while (randomNum1 == randomNum2); double GameTime = 1000; // 游戏时间 double gametime; // 用户所用时间 if (randomNum3 == 1) { n = randomNum1 + randomNum2; cout << i + 1 << " " << randomNum1; cout << "+"; cout << randomNum2 << "=" << endl; } if (randomNum3 == 2) { n = randomNum1 - randomNum2; if (n < 0) { cout << i + 1 << " " << randomNum2; cout << "-"; cout << randomNum1 << "=" << endl; n = randomNum2 - randomNum1; } else { cout << i + 1 << " " << randomNum1; cout << "-"; cout << randomNum2 << "=" << endl; n = randomNum1 - randomNum2; } } if (randomNum3 == 3) { n = randomNum1 * randomNum2; while (n >= 1000) { n = randomNum1 * randomNum2; randomNum1--; randomNum2--; } n = randomNum1 * randomNum2; cout << i + 1 << " " << randomNum1; cout << "*"; cout << randomNum2 << "=" << endl; } if (randomNum3 == 4) { do { randomNum1 = dist3(gen1); // 生成随机数 randomNum2 = dist4(gen2); } while (randomNum1 % randomNum2 != 0 && randomNum1 < randomNum2 && randomNum1 != 0 && randomNum2 != 0); n = randomNum1 / randomNum2; cout << i + 1 << " " << randomNum1 << "/" << randomNum2 << "=" << endl; } start = clock(); cin >> a[i]; finish = clock(); gametime = (double)(finish - start) / CLOCKS_PER_SEC; if (gametime >= GameTime) { cout << "很遗憾,未能在规定时间内得出答案 正确结果" << n << endl; f++; continue; } if (a[i] == n) { cout << "true" << endl; t++; } else { cout << "false 正确结果 " << n << endl; f++; } } cout << "错题数:" << f << endl; cout << "正确率:" << f1(t) << endl; return 0; }
import java.util.Random; import java.util.Scanner; public class Main { public static double f1(double a) { return a / 30; } public static void main(String[] args) { long start, finish; // 挂钟时间 double t = 0; int f = 0; Random rd = new Random(); // 用于获得随机种子 int[] a = new int[30]; for (int i = 0; i < 30; i++) { int randomNum1, randomNum2, randomNum3, n; do { randomNum1 = rd.nextInt(90) + 10; // 生成随机数 randomNum2 = rd.nextInt(90) + 10; randomNum3 = rd.nextInt(4) + 1; } while (randomNum1 == randomNum2); double GameTime = 1000; // 游戏时间 double gametime; // 用户所用时间 if (randomNum3 == 1) { n = randomNum1 + randomNum2; System.out.print((i + 1) + " " + randomNum1); System.out.print("+"); System.out.println(randomNum2 + "="); } if (randomNum3 == 2) { n = randomNum1 - randomNum2; if (n < 0) { System.out.print((i + 1) + " " + randomNum2); System.out.print("-"); System.out.println(randomNum1 + "="); n = randomNum2 - randomNum1; } else { System.out.print((i + 1) + " " + randomNum1); System.out.print("-"); System.out.println(randomNum2 + "="); n = randomNum1 - randomNum2; } } if (randomNum3 == 3) { n = randomNum1 * randomNum2; while (n >= 1000) { n = randomNum1 * randomNum2; randomNum1--; randomNum2--; } n = randomNum1 * randomNum2; System.out.print((i + 1) + " " + randomNum1); System.out.print("*"); System.out.println(randomNum2 + "="); } if (randomNum3 == 4) { do { randomNum1 = rd.nextInt(90) + 10; // 生成随机数 randomNum2 = rd.nextInt(3) + 1; } while (randomNum1 % randomNum2 != 0 && randomNum1 < randomNum2 && randomNum1 != 0 && randomNum2 != 0); n = randomNum1 / randomNum2; System.out.println((i + 1) + " " + randomNum1 + "/" + randomNum2 + "="); } start = System.nanoTime(); Scanner scanner = new Scanner(System.in); a[i] = scanner.nextInt(); finish = System.nanoTime(); gametime = (double) (finish - start) / 1_000_000_000; if (gametime >= GameTime) { System.out.println("很遗憾,未能在规定时间内得出答案 正确结果 " + n); f++; continue; } if (a[i] == n) { System.out.println("true"); t++; } else { System.out.println("false 正确结果 " + n); f++; } } System.out.println("错题数:" + f); System.out.println("正确率:" + f1(t)); } }
课堂上写的C++,回来改成java了。
因为就生成30个随机题目,不需要保证题目不重复。因为可能性太低了,如果一定要,会浪费大量时间,所以我在此删去了这段代码