zoukankan      html  css  js  c++  java
  • 自动生成计算题

    最近老师总是让出一些计算题,于是用haskell写了一个。

    -- file: make_math2.hs
    -- 出n道三个参数的加法
    -- 452 + 990 + 130=1572
    -- 使用方法: gs n x y
    --              n 表示需要多少道题
    --              x 表示最小值
    --              y 表示最大值
    --       比如 gs 10 100 999 表示出10道 加法
    --       gs'会过滤掉被10整除的数字,这样难一点
    
    import System.Random
    import System.IO.Unsafe
    
    drawInt :: Int -> Int -> IO Int
    drawInt x y = getStdRandom (randomR (x,y))
    
    random_list :: (Eq a, Num a) => a -> Int -> Int -> IO [Int]
    random_list 0 _ _ = return []
    random_list n x y = do
        a <- drawInt x y
        rest <- (random_list (n-1) x y)
        return (a : rest)
    
    
    math_practice n x y  = zip [1..] $ map trans_formula (mix_list (init_val (n*3) x y ))
        where mix_list [] = []
              mix_list (x:y:z:xs) = (x,y,z) : mix_list xs 
              init_val n x y = unsafePerformIO $ random_list n x y
    
              trans_formula (x,y,z) = ((add_str x y z) , (add_str' x y z))
    
              add_str x y z = show x ++ " + " ++ show y  ++ " + " ++ show z ++ "= "
              add_str' x y z = show x ++ " + " ++ show y ++  " + "  ++ show z ++ "=" ++ show (x+y+z)
    
    
    math_practice' n x y  = zip [1..] $ map trans_formula $ mix_list 
                    $ take (n*3) $ filter (\x -> (x `mod` 10) /= 0) $ init_val (n*9) x y
        where mix_list [] = []
              mix_list (x:y:z:xs) = (x,y,z) : mix_list xs 
              init_val n x y = unsafePerformIO $ random_list n x y
    
              trans_formula (x,y,z) = ((add_str x y z) , (add_str' x y z))
    
              add_str x y z = show x ++ " + " ++ show y  ++ " + " ++ show z ++ "= "
              add_str' x y z = show x ++ " + " ++ show y ++  " + "  ++ show z ++ "=" ++ show (x+y+z)
    gs n x y = do
        mapM_ putStrLn [ show a ++ ". " ++ b| (a,(b,c))<-temp_list]
        putStrLn " "
        mapM_ putStrLn [ show a ++ ". " ++ c| (a,(b,c))<-temp_list]
        where temp_list = math_practice' n x y
    
    gs' n x y = do
        mapM_ putStrLn [ b| (a,(b,c))<-temp_list]
        putStrLn " "
        where temp_list = math_practice' n x y
    
    {-main = do-}
        {-gs 10 100 999-}

    使用的例子

    > gs 10 10 99
    1. 94 + 79 + 26= 
    2. 34 + 65 + 73= 
    3. 88 + 29 + 24= 
    4. 54 + 93 + 29= 
    5. 32 + 79 + 82= 
    6. 24 + 55 + 67= 
    7. 46 + 16 + 64= 
    8. 43 + 39 + 84= 
    9. 38 + 89 + 62= 
    10. 91 + 51 + 83= 
     
    1. 94 + 79 + 26=199
    2. 34 + 65 + 73=172
    3. 88 + 29 + 24=141
    4. 54 + 93 + 29=176
    5. 32 + 79 + 82=193
    6. 24 + 55 + 67=146
    7. 46 + 16 + 64=126
    8. 43 + 39 + 84=166
    9. 38 + 89 + 62=189
    10. 91 + 51 + 83=225
  • 相关阅读:
    android学习——android项目的的目录结构
    android学习——android项目的的目录结构
    LA 4670 Dominating Patterns (AC自动机)
    HDU 2089 不要62 (递推+暴力或者数位DP)
    HDU 2504 又见GCD (最大公因数+暴力)
    HDU 2136 Largest prime factor (素数打表。。。)
    HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
    HDU 3177 Crixalis's Equipment (贪心,差值)
    HDU 1718 Rank (排序)
    HDU 1716 排列2 (格式问题+排列)
  • 原文地址:https://www.cnblogs.com/bailiang/p/2540947.html
Copyright © 2011-2022 走看看