zoukankan      html  css  js  c++  java
  • 三十道随机算法

    9.29

    一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。

    课堂测试1:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件”

    课堂测试2:(1)题目避免重复; (2)可定制(数量/打印方式);

    课堂测试3: 1、可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找系统是否优化的余地; 2、定制操作数的个数;3、定制是否有乘除法; 4、定制是否有括号(随机加入); 5 、定制数值范围(确定操作数的取值范围)

    目前只能实现到可定制,仍需改进。

    package ClassTest;
    import java.util.Random;
    import java.util.Scanner;
    public class math2 {
        static Scanner sc = null;
        public static int s1 = new Random().nextInt(100);
        public static int s2 = new Random().nextInt(100);
        public static int s3 = new Random().nextInt(100);
        public static int a[][]=new int[100000][2] ;//用于从操作数
     
        public static int s[]=new int[100000];//用于存操作符
     
        public static void main(String[] args) {
     
            sc = new Scanner(System.in);
            System.out.println("请输入题数:");
            int n=sc.nextInt();
            System.out.println("请输入每行的题数:");
            int m=sc.nextInt();
     
            for(int i=0;i<n;i++) {
                if((i)%m==0)
                    System.out.print("\n");
                System.out.print((i+1)+":");
     
                //产生随机数
                s1 = new Random().nextInt(100);
                System.out.print(s1);
                s2 = new Random().nextInt(100);
                s3 = new Random().nextInt(100);            
     
                //检查重复
                    a[i][0]=s1;
                    a[i][1]=s3;
                    s[i]=s2;
                for(int j=0;j<i;j++) {
                    if(a[j][0]==s1&&a[j][1]==s3&&s[j]==s2) {
                        s3 = new Random().nextInt(100);
                        while(s3==a[i][1]) {
                            s3 = new Random().nextInt(100);
                    }
                    s3 = new Random().nextInt(100);
                    while(s3==a[i][1]) {
     
                    }
                }}
     
                //判断操作符
                if(s2%4==0)
                    System.out.print("+");
                if(s2%4==1) {
                    System.out.print("-");
     
                        while(s1<s3) {
                            s3 = new Random().nextInt(100);
                        }
     
                }
     
                if(s2%4==2) {
                    System.out.print("*");
                    while(s1*s3>=100) {
                        //s1 = new Random().nextInt(100);
                        s3 = new Random().nextInt(100);
                    }
                }
     
                if(s2%4==3) {
                    System.out.print("/");
                    while(s3!=0&&s1/s3!=0){
                        //s1 = new Random().nextInt(100);
                        s3 = new Random().nextInt(100);
                    }
                }
     
                System.out.print(s3);
                System.out.print("=");
                System.out.print(" "+'\t');
            }
     
        }
     
    }

    程序截图:

  • 相关阅读:
    大型项目前端架构浅谈
    图标使用新姿势- react 按需引用 svg 的实现
    为什么json 不能使用 int64类型
    SSL证书对SEO网站优有什么影响?使用HTTPS的SEO优势
    web安全测试必须注意的五个方面
    在小程序中实现 Mixins 方案
    网站web前端常见的优化措施
    前端 HTML空格的六种方式
    如何正确选型,React Native 还是 Native?
    CSS3 渐变(Gradients)
  • 原文地址:https://www.cnblogs.com/wangdayang/p/13779682.html
Copyright © 2011-2022 走看看