Skip to content

Instantly share code, notes, and snippets.

@ajalab
Created September 30, 2011 13:43
Show Gist options
  • Save ajalab/1253770 to your computer and use it in GitHub Desktop.
Save ajalab/1253770 to your computer and use it in GitHub Desktop.
Haskellで小町算
import qualified Control.Exception as C
data Opr = Add | Sub | Mul | Div | Cat deriving Eq
pr :: Opr -> Int
pr Add = 1
pr Sub = 1
pr Mul = 2
pr Div = 2
pr Cat = 3
instance Ord Opr where
compare x y = compare (pr x) (pr y)
instance Show Opr where
show Add = " + "
show Sub = " - "
show Mul = " * "
show Div = " / "
show Cat = ""
data Expr = Expr { opr :: Opr, a :: Expr, b :: Expr } |
Number Int
instance Show Expr where
show (Expr o x y) =
case x of
(Number _) -> show x
(Expr {opr = o'}) -- x@(A o' B) o M
| o' == Cat || o <= o' -> show x
otherwise -> "(" ++ show x ++ ")"
++ show o ++
case y of
(Number _) -> show y
(Expr {opr = o'}) -- M o y@(A o' B)
| o' == Cat || o' >= o -> show y
otherwise -> "(" ++ show y ++ ")"
show (Number n) = show n
eval :: Expr -> Rational
eval (Number a) = fromIntegral (a :: Int)
eval (Expr o a b) = case o of
Add -> eval a + eval b
Sub -> eval a - eval b
Mul -> eval a * eval b
Div -> eval a / eval b
Cat -> eval a * 10 + eval b
createTree :: [Expr] -> [Expr]
createTree [] = []
createTree [x] = [x]
createTree (x:y:[]) = x `exprs` y
createTree (x:y:ys) = concatMap (createTree . (:ys)) (x `exprs` y)
++ concatMap (x `exprs`) (createTree (y:ys))
baseList = [Add, Sub, Mul, Div]
exprs :: Expr -> Expr -> [Expr]
exprs x y = case y of
(Expr {opr = o}) -> map (\o' -> Expr o' x y) . filter (\o' -> o `compare` o' /= EQ) $ baseList
(Number _) -> map (\o' -> Expr o' x y) $ case x of
(Number _) -> Cat:baseList
(Expr {opr = o'})
| o' == Cat -> Cat:baseList
| otherwise -> baseList
komachi :: Int -> [(String, Rational)]
komachi n = let a = map (\x -> Number x) [1 .. n]
in map (\x -> (show x, eval x)) $ createTree a
main :: IO ()
main = let a = komachi 9
ans = 100 :: Int
in mapM_ (\(s, ans') -> C.handle
(\(C.ErrorCall _) -> return ())
(if ans' == fromIntegral ans
then putStrLn (s ++ " = " ++ (show ans))
else return ())
) a
1234 + 5 - 67 * (8 + 9) = 100
123 - 4 - 5 - 6 - 7 + 8 - 9 = 100
123 * 4 + 56 * 7 * (8 - 9) = 100
123 * 4 + 56 * 7 / (8 - 9) = 100
123 - 4 * (5 + 6 / (7 - 8 + 9)) = 100
12 + 3 + 4 + 5 - 6 - 7 + 89 = 100
(12 + 3 + 4 + 5) / 6 + 7 + 89 = 100
(12 + 3 + 4 + 5) / 6 * 7 + 8 * 9 = 100
(12 + 3 - 4) * 5 + (6 + 7 - 8) * 9 = 100
(12 + 3) * 4 - 5 + (6 + 7 - 8) * 9 = 100
(12 + 3) * 4 / 5 + 6 - 7 + 89 = 100
12 + 3 - 45 * (6 - 7 - 8 / 9) = 100
(12 + 3) * (4 + 5 - (6 + 7 + 8) / 9) = 100
(12 + 3) * (4 - 5 + 6 + (7 + 8) / 9) = 100
12 + 3 - (4 - 5) * (6 + 7 + 8 * 9) = 100
(12 + 3) * (4 - 5 / (6 - 7 / 8 * 9)) = 100
(12 - 3 + 4 + 5) * 6 - 7 + 8 - 9 = 100
12 - 3 + 4 - (5 - 6) * (78 + 9) = 100
12 - 3 - 4 + 5 - 6 + 7 + 89 = 100
(12 - 3 - 4 + 5 - 6) * 7 + 8 * 9 = 100
(12 - 3 - 4) * 5 / 6 * (7 + 8 + 9) = 100
(12 - 3 - 4) * (5 + 6 - (7 - 8) * 9) = 100
(12 - 3 - 4) * (5 * 6 + 7 - 8 - 9) = 100
(12 - 3) * 4 + 5 - 6 - 7 + 8 * 9 = 100
(12 - 3) * 4 / 5 * (6 + 7 + 8 / 9) = 100
(12 - 3) * (4 - 5 + 6 + 7 - 8 / 9) = 100
(12 - 3) * (4 + 56 / 7 * 8 / 9) = 100
(12 - 3) * (4 - (5 - 6 - 7) * 8 / 9) = 100
(12 * 3 + 4) * (5 / 6 + (7 + 8) / 9) = 100
12 * 3 + 4 + 5 * (6 + 7 + 8 - 9) = 100
12 * 3 - 4 + 5 + (6 - 7 + 8) * 9 = 100
12 * 3 - 4 - 5 - 6 + 7 + 8 * 9 = 100
(12 * 3 - 4) * 5 + 6 * (7 - 8 - 9) = 100
12 * 3 - 4 + (5 + 6 - 7) * (8 + 9) = 100
(12 * 3 - 4) * (5 + 6 - 7 / 8 * 9) = 100
(12 * 3 / 4 - 5) * (6 * 7 - 8 - 9) = 100
12 * 3 / 4 * (5 + 6 - (7 - 8) / 9) = 100
12 * 3 * (4 - 5 + (6 * 7 - 8) / 9) = 100
(12 / 3 + 4 - 5) * 6 - 7 + 89 = 100
(12 / 3 + 4) * 5 - 6 * (7 - 8 - 9) = 100
(12 / 3 * 4 + 5 + 6) * 7 - 89 = 100
12 / 3 * 4 - 5 - (6 - 7) * 89 = 100
(12 / 3 / 4 - 5 - 6) * (7 - 8 - 9) = 100
12 / 3 * (4 + 5 + 6 - 7 + 8 + 9) = 100
12 / 3 * ((4 * 5 - 6) / 7 * 8 + 9) = 100
12 / 3 - 4 * ((5 - 6) * 7 - 8 - 9) = 100
12 / 3 + 4 * (5 * 6 - 7 - 8 + 9) = 100
12 * ((3 + 4 - 5) / 6 + 7 - 8 + 9) = 100
12 * (3 + 4 + (5 + 6 - 7 + 8) / 9) = 100
12 * (3 + 4 - 5 / (6 * 7 / 8 - 9)) = 100
12 * (3 - 4 + 5 + 6 - (7 + 8) / 9) = 100
12 - (3 - 4 + 5) * (67 - 89) = 100
12 - (3 - 4 - 5) * (6 + 78 / 9) = 100
12 - ((3 - 4) * 5 - 6) * (7 - 8 + 9) = 100
12 * (3 - 4 + 56 / (7 + 8 - 9)) = 100
12 + (3 * 4 + 5 - 6) * (7 - 8 + 9) = 100
12 * (3 * 4 * 5 / 6 - (7 + 8) / 9) = 100
12 + 3 * 4 / 5 * 6 * (7 - 8 / 9) = 100
12 + 3 * 4 * (5 + (6 + 7 + 8) / 9) = 100
12 * (3 - 4 * 5 / (6 * 7 / 8 - 9)) = 100
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 * 9 = 100
(1 + 2 + 3 + 4 + 5) * 6 - 7 + 8 + 9 = 100
(1 + 2 + 3 + 4) / 5 * (67 - 8 - 9) = 100
(1 + 2 + 3 + 4) * (56 / 7 / 8 + 9) = 100
1 + 2 + 3 + 4 + (5 + 6 + 7 - 8) * 9 = 100
(1 + 2 + 3 + 4) * ((5 - 6) * 7 + 8 + 9) = 100
1 + 2 + 3 + 4 - 5 * (6 - 7 - 8 - 9) = 100
(1 + 2 + 3 - 4) * 5 - 6 + 7 + 89 = 100
((1 + 2 + 3 - 4) * 5 - 6) * 7 + 8 * 9 = 100
(1 + 2 + 3 - 4) * (56 - 7 - 8 + 9) = 100
(1 + 2 + 3 - 4) * (5 + (6 + 7 - 8) * 9) = 100
(1 + 2 + 3) * 4 + 5 + 6 - 7 + 8 * 9 = 100
(1 + 2 + 3) * 4 - 5 - (6 - 7 - 8) * 9 = 100
((1 + 2 + 3) / 4 + 5 + 6) * (7 - 8 + 9) = 100
(1 + 2 + 3) * (4 + 5 + 6 + (7 + 8) / 9) = 100
(1 + 2 - 3 + 4) / 5 * (6 + 7 * (8 + 9)) = 100
(1 + 2 - 3 + 4) * (56 / 7 + 8 + 9) = 100
1 + 2 - 3 + 4 - (5 - 6) * (7 + 89) = 100
(1 + 2 - 3 - 4) * 5 * (67 - 8 * 9) = 100
(1 + 2 - 3 - 4) * (5 - 6 - 7 - 8 - 9) = 100
1 + 2 - 3 - 4 * 5 * (67 - 8 * 9) = 100
1 + 2 - 3 + 4 / 5 * (6 + 7 * (8 + 9)) = 100
1 + 2 - 3 + 4 * (56 / 7 + 8 + 9) = 100
1 + 2 - 3 - 4 * (5 - 6 - 7 - 8 - 9) = 100
((1 + 2) * 3 + 4 + 5) * 6 - 7 + 8 - 9 = 100
(1 + 2) * 3 + 4 - (5 - 6) * (78 + 9) = 100
(1 + 2) * 3 - 4 + 5 - 6 + 7 + 89 = 100
((1 + 2) * 3 - 4 + 5 - 6) * 7 + 8 * 9 = 100
((1 + 2) * 3 - 4) * 5 / 6 * (7 + 8 + 9) = 100
((1 + 2) * 3 - 4) * (5 + 6 - (7 - 8) * 9) = 100
((1 + 2) * 3 - 4) * (5 * 6 + 7 - 8 - 9) = 100
(1 + 2) * 3 * 4 + 5 - 6 - 7 + 8 * 9 = 100
(1 + 2) * 3 * 4 / 5 * (6 + 7 + 8 / 9) = 100
(1 + 2) * 3 * (4 - 5 + 6 + 7 - 8 / 9) = 100
(1 + 2) * 3 * (4 + 56 / 7 * 8 / 9) = 100
(1 + 2) * 3 * (4 - (5 - 6 - 7) * 8 / 9) = 100
(1 + 2) / 3 + 4 + 5 - 6 + 7 + 89 = 100
((1 + 2) / 3 + 4 + 5 - 6) * 7 + 8 * 9 = 100
((1 + 2) / 3 + 4) * 5 / 6 * (7 + 8 + 9) = 100
((1 + 2) / 3 + 4) * (5 + 6 - (7 - 8) * 9) = 100
((1 + 2) / 3 + 4) * (5 * 6 + 7 - 8 - 9) = 100
((1 + 2) / 3 - 4 + 5) * (67 - 8 - 9) = 100
(1 + 2) / 3 * 4 / 5 * (6 + 7 * (8 + 9)) = 100
(1 + 2) / 3 * 4 * (56 / 7 + 8 + 9) = 100
(1 + 2) / 3 * 4 - (5 - 6) * (7 + 89) = 100
(1 + 2) / 3 + (4 * 5 + 6 - 7 - 8) * 9 = 100
(1 + 2) / 3 * (4 - (5 - 6) * (7 + 89)) = 100
(1 + 2) * (34 - 5 + 6 - (7 + 8) / 9) = 100
1 + 2 - (3 - 4) * (56 / 7 + 89) = 100
1 + 2 + (3 - 4) * (5 - 6 - 7 - 89) = 100
(1 + 2) * ((3 * 4 - 5) * 6 - 78 / 9) = 100
1 - 2 + 3 + 4 + 5 - (6 - 7) * 89 = 100
(1 - 2 + 3 - 4 + 5) * 6 - 7 + 89 = 100
1 - 2 + 3 - 4 + (5 - 6 + 7) * (8 + 9) = 100
((1 - 2 + 3) * 4 - 5) * 6 - 7 + 89 = 100
(1 - 2 + 3) * 4 * 5 - 6 * (7 - 8 - 9) = 100
(1 - 2 + 3) * (4 * 5 + 6 + 7 + 8 + 9) = 100
1 - 2 + 3 - (4 * 5 - 6) * 7 * (8 - 9) = 100
1 - 2 + 3 - (4 * 5 - 6) * 7 / (8 - 9) = 100
1 - 2 - 3 - 4 - 5 - 6 + 7 * (8 + 9) = 100
1 - 2 - 3 - 4 + (5 + 6 - 7 + 8) * 9 = 100
((1 - 2 - 3) / 4 + 5) * (6 * 7 - 8 - 9) = 100
(1 - 2 - 3) * ((4 + 5) * 6 - 7 - 8 * 9) = 100
(1 - 2 - 3) * ((4 - 5 + 6 - 7) * 8 - 9) = 100
(1 - 2 - 3) * ((4 - 5) * 6 * 7 + 8 + 9) = 100
(1 - 2 - 3) * (4 * 5 - (6 + 7 - 8) * 9) = 100
1 - 2 - 3 - 4 * (56 + 7 - 89) = 100
((1 - 2) * 3 + 4 - 5 - 6) * (7 - 8 - 9) = 100
(1 - 2) * 3 - 4 + 5 + 6 + 7 + 89 = 100
((1 - 2) * 3 - 4 + 5 + 6) * 7 + 8 * 9 = 100
(1 - 2) * 3 * 4 * 5 / 6 * (7 - 8 - 9) = 100
((1 - 2) / 3 + 4) * 5 * 6 + 7 - 8 - 9 = 100
((1 - 2) / 3 - 4 + 5) * 6 + 7 + 89 = 100
((1 - 2) / 3 - 4 + 5) * 6 * 7 + 8 * 9 = 100
(1 - 2) * (34 - 5 + 6 - (7 + 8) * 9) = 100
(1 - 2) * (3 + 4 - 5 - 6 - 7 - 89) = 100
(1 - 2) * ((3 + 4 - 5 - 6) * 7 - 8 * 9) = 100
(1 * 2 + 3 + 4 - 5) * (6 * 7 - 8 - 9) = 100
(1 * 2 + 3 + 4) * (5 + 6 - (7 - 8) / 9) = 100
(1 * 2 + 3 - 4 - 5 - 6) * (7 - 8 - 9) = 100
((1 * 2 + 3) * 4 + 5) / 6 * (7 + 8 + 9) = 100
(1 * 2 + 3) * 4 - 5 + 6 + 7 + 8 * 9 = 100
((1 * 2 + 3) * 4 - 5) * 6 - 7 + 8 + 9 = 100
(1 * 2 + 3) * 4 * 5 + 6 - 7 - 8 + 9 = 100
(1 * 2 + 3) * 4 * 5 - 6 + 7 + 8 - 9 = 100
(1 * 2 + 3) * 4 * 5 * 6 / (7 + 8 - 9) = 100
(1 * 2 + 3) * 4 * 5 / 6 * (7 + 8 - 9) = 100
(1 * 2 + 3) * 4 * 5 * ((6 - 7) * 8 + 9) = 100
(1 * 2 + 3) * 4 * 5 / ((6 - 7) * 8 + 9) = 100
(1 * 2 + 3) * 4 / 5 * (6 * 7 - 8 - 9) = 100
(1 * 2 + 3) * 4 * (5 + 6 - 7 - 8 + 9) = 100
(1 * 2 + 3) * 4 * ((5 + 6) * 7 - 8 * 9) = 100
(1 * 2 + 3) * 4 * (5 - 6 + 7 + 8 - 9) = 100
(1 * 2 + 3) * 4 + 5 / 6 * (7 + 89) = 100
(1 * 2 + 3) * 4 + 5 * (6 - 7 + 8 + 9) = 100
((1 * 2 + 3) / 4 + 5) / 6 * (7 + 89) = 100
((1 * 2 + 3) / 4 + 5) * (6 - 7 + 8 + 9) = 100
(1 * 2 + 3) / 4 * 5 / 6 * (7 + 89) = 100
(1 * 2 + 3) / 4 * 5 * (6 - 7 + 8 + 9) = 100
(1 * 2 + 3) / 4 * (56 + 7 + 8 + 9) = 100
(1 * 2 + 3) / 4 * (56 / 7 + 8 * 9) = 100
(1 * 2 + 3) * ((4 + 5 - 6) * 7 + 8 - 9) = 100
1 * 2 + 3 - (4 - 5) * ((6 + 7) * 8 - 9) = 100
(1 * 2 + 3) * (4 * 5 + 6 - 7 - 8 + 9) = 100
(1 * 2 + 3) * (4 * 5 - 6 + 7 + 8 - 9) = 100
(1 * 2 + 3) / (4 / 5 - 6 / (7 - 8 + 9)) = 100
(1 * 2 + 3) * (4 + (5 + 6 + 7) * 8 / 9) = 100
1 * 2 - 3 + 4 - 5 + 6 + 7 + 89 = 100
(1 * 2 - 3 + 4 - 5 + 6) * 7 + 8 * 9 = 100
(1 * 2 - 3 + 4) * 5 + 6 + 7 + 8 * 9 = 100
(1 * 2 - 3 + 4) * 5 * 6 - 7 + 8 + 9 = 100
(1 * 2 - 3 - 4) * 5 + 6 + 7 * (8 + 9) = 100
(1 * 2 - 3 - 4) * 5 * (6 + 7 - 8 - 9) = 100
(1 * 2 - 3) * 4 * 5 * (67 - 8 * 9) = 100
(1 * 2 - 3) * 4 * (5 - 6 - 7 - 8 - 9) = 100
1 * 2 * 3 + 4 + 5 + 6 + 7 + 8 * 9 = 100
(1 * 2 * 3 + 4 + 5) * 6 - 7 + 8 + 9 = 100
(1 * 2 * 3 + 4) / 5 * (67 - 8 - 9) = 100
(1 * 2 * 3 + 4) * (56 / 7 / 8 + 9) = 100
1 * 2 * 3 + 4 + (5 + 6 + 7 - 8) * 9 = 100
(1 * 2 * 3 + 4) * ((5 - 6) * 7 + 8 + 9) = 100
1 * 2 * 3 + 4 - 5 * (6 - 7 - 8 - 9) = 100
(1 * 2 * 3 - 4) * 5 - 6 + 7 + 89 = 100
((1 * 2 * 3 - 4) * 5 - 6) * 7 + 8 * 9 = 100
(1 * 2 * 3 - 4) * (56 - 7 - 8 + 9) = 100
(1 * 2 * 3 - 4) * (5 + (6 + 7 - 8) * 9) = 100
1 * 2 * 3 * 4 + 5 + 6 - 7 + 8 * 9 = 100
1 * 2 * 3 * 4 - 5 - (6 - 7 - 8) * 9 = 100
(1 * 2 * 3 / 4 + 5 + 6) * (7 - 8 + 9) = 100
1 * 2 * 3 * (4 + 5 + 6 + (7 + 8) / 9) = 100
1 * 2 / 3 * (4 + 5 + 6 + (7 + 8) * 9) = 100
1 * 2 / 3 * ((4 + 5) * 6 + 7 + 89) = 100
1 * 2 / 3 + 4 * (5 / 6 + 7 + 8 + 9) = 100
1 * 2 * (34 + (5 + 6 + 7) * 8 / 9) = 100
1 * 2 * (3 + 4 - 5 + 6 * (7 - 8 + 9)) = 100
1 * 2 * ((3 + 4) * 5 + 6 - (7 - 8) * 9) = 100
1 * 2 * ((3 * 4 - 5) * 6 + 7 - 8 + 9) = 100
1 * 2 + (3 * 4 - 5) * (6 + 7 - 8 + 9) = 100
1 * 2 - 3 * 4 * (5 / 6 + (7 - 8) * 9) = 100
1 * 2 * (3 - ((4 + 5) * 6 - 7) * (8 - 9)) = 100
1 * 2 * (3 - ((4 + 5) * 6 - 7) / (8 - 9)) = 100
1 * 2 * (3 + (4 - 5) * (6 * 7 - 89)) = 100
(1 / 2 + 3 + 4) * (5 + (67 + 8) / 9) = 100
(1 / 2 + 3 - 4 + 5) * 6 * 7 - 89 = 100
(1 / 2 + 3) * 4 + 5 - (6 - 7 - 8) * 9 = 100
(1 / 2 + 3) * 4 * 5 + 6 + 7 + 8 + 9 = 100
(1 / 2 - 3 + 4 + 5 + 6) * (7 - 8 + 9) = 100
(1 / 2 - 3) * 4 * 5 * (6 - 7 + 8 - 9) = 100
(1 / 2 - 3) * 4 / 5 + 6 + 7 + 89 = 100
((1 / 2 - 3) * 4 / 5 + 6) * 7 + 8 * 9 = 100
(1 / 2 - 3) * 4 - (5 + 6) * (7 - 8 - 9) = 100
(1 / 2 - 3) * 4 * ((5 - 6 - 7) / 8 - 9) = 100
(1 / 2 - 3) * 4 * (5 - 6 + (7 - 8) * 9) = 100
(1 / 2 - 3) * 4 - 5 * (67 - 89) = 100
(1 / 2 - 3) * (45 - 6 - 7 - 8 * 9) = 100
(1 / 2 - 3) * (4 * 5 + 6 * (7 - 8 - 9)) = 100
(1 / 2 * 3 + 4) * 5 * 6 + 7 - 8 * 9 = 100
(1 / 2 * 3 - 4) * (56 - 7 - 89) = 100
(1 / 2 * 3 - 4) * (5 - (6 + 7 - 8) * 9) = 100
1 / 2 * 3 * 4 + 5 - (6 - 7) * 89 = 100
(1 / 2 / 3 + 4) * (5 * 6 - 7 - 8 + 9) = 100
1 / 2 / 3 * 4 * 5 * (6 + 7 + 8 + 9) = 100
(1 / 2 / 3 / 4 - 5 + 6) * (7 + 89) = 100
1 / 2 * (34 * 5 + 6 + 7 + 8 + 9) = 100
1 / 2 * ((3 + 4) * 5 * 6 + 7 - 8 - 9) = 100
1 * (234 - 5 + 6 - (7 + 8) * 9) = 100
1 + (23 + 4 + 5 - 6 - 7 - 8) * 9 = 100
1 - (23 + 4 - 5) / (6 - 7 * 8 / 9) = 100
1 * (23 - 4 + 5 - 6 - 7 + 89) = 100
1 * ((23 - 4 + 5) / 6 + 7 + 89) = 100
1 - ((23 - 4 + 5) / 6 - 7 - 8) * 9 = 100
1 * ((23 - 4 + 5) / 6 * 7 + 8 * 9) = 100
1 * (23 * 4 + 5 - 6 - (7 - 8) * 9) = 100
1 * (23 * 4 - 5 + 6 - 7 * (8 - 9)) = 100
1 * (23 * 4 - 5 + 6 - 7 / (8 - 9)) = 100
1 + (23 * 4 - 5 - 6 + 7) / 8 * 9 = 100
1 * (23 * 4 - 5 - (6 + 7) * (8 - 9)) = 100
1 * (23 * 4 - 5 - (6 + 7) / (8 - 9)) = 100
1 * (23 * 4 - 56 / 7 * (8 - 9)) = 100
1 * (23 * 4 - 56 / 7 / (8 - 9)) = 100
1 * (23 * 4 + (5 - 6 - 7) * (8 - 9)) = 100
1 * (23 * 4 + (5 - 6 - 7) / (8 - 9)) = 100
1 * (23 * 4 - (5 - 6) * (7 - 8 + 9)) = 100
1 * (23 * 4 - 5 / (67 / 8 - 9)) = 100
1 + (2 + 3 + 4 - 5 + 6 - 7 + 8) * 9 = 100
1 - (2 + 3 + 4) * (5 - 6 + 7 - 8 - 9) = 100
1 + (2 + 3 - 4 + 5 + 6 + 7 - 8) * 9 = 100
1 - (2 + 3 - 4 - 5 - 6 + 7 - 8) * 9 = 100
1 * ((2 + 3) * 4 - 5 + 6 + 7 + 8 * 9) = 100
1 + (((2 + 3) * 4 - 5 + 6) / 7 + 8) * 9 = 100
1 * (((2 + 3) * 4 - 5) * 6 - 7 + 8 + 9) = 100
1 * ((2 + 3) * 4 * 5 + 6 - 7 - 8 + 9) = 100
1 - ((2 + 3) * 4 * 5 + 6 - 7) * (8 - 9) = 100
1 - ((2 + 3) * 4 * 5 + 6 - 7) / (8 - 9) = 100
1 * ((2 + 3) * 4 * 5 - 6 + 7 + 8 - 9) = 100
1 + ((2 + 3) * 4 / 5 + 6 - 7 + 8) * 9 = 100
1 * ((2 + 3) * 4 + 5 / 6 * (7 + 89)) = 100
1 * ((2 + 3) * 4 + 5 * (6 - 7 + 8 + 9)) = 100
1 * (2 + 3 - (4 - 5) * ((6 + 7) * 8 - 9)) = 100
1 * (2 - 3 + 4 - 5 + 6 + 7 + 89) = 100
1 - (2 - 3 + 4 - 5 + 6 - 7 - 8) * 9 = 100
1 * ((2 - 3 + 4 - 5 + 6) * 7 + 8 * 9) = 100
1 - ((2 - 3 + 4 - 5) * 6 - 7 + 8) * 9 = 100
1 * ((2 - 3 + 4) * 5 + 6 + 7 + 8 * 9) = 100
1 + (((2 - 3 + 4) * 5 + 6) / 7 + 8) * 9 = 100
1 * ((2 - 3 + 4) * 5 * 6 - 7 + 8 + 9) = 100
1 + (2 - 3 - 4 - 5 + 6 + 7 + 8) * 9 = 100
1 * ((2 - 3 - 4) * 5 + 6 + 7 * (8 + 9)) = 100
1 - ((2 - 3) * 4 * 5 - 6 + 7 + 8) * 9 = 100
1 + (2 - 3) * (45 - 6 * (7 + 8 + 9)) = 100
1 - (2 - 3) * (4 + 5 - 6 + 7 + 89) = 100
1 - (2 - 3) * ((4 * 5 - 6) * 7 - 8 + 9) = 100
1 * (2 * 3 + 4 + 5 + 6 + 7 + 8 * 9) = 100
1 + ((2 * 3 + 4 + 5 + 6) / 7 + 8) * 9 = 100
1 * ((2 * 3 + 4 + 5) * 6 - 7 + 8 + 9) = 100
1 + ((2 * 3 + 4) / 5 - 6 + 7 + 8) * 9 = 100
1 + ((2 * 3 + 4) / 5 * 6 + 7 - 8) * 9 = 100
1 * (2 * 3 + 4 + (5 + 6 + 7 - 8) * 9) = 100
1 * (2 * 3 + 4 - 5 * (6 - 7 - 8 - 9)) = 100
1 * ((2 * 3 - 4) * 5 - 6 + 7 + 89) = 100
1 - ((2 * 3 - 4) * 5 - 6 - 7 - 8) * 9 = 100
1 * (((2 * 3 - 4) * 5 - 6) * 7 + 8 * 9) = 100
1 * (2 * 3 * 4 + 5 + 6 - 7 + 8 * 9) = 100
1 * (2 * 3 * 4 - 5 - (6 - 7 - 8) * 9) = 100
1 - ((2 * 3 / 4 - 5) * 6 / 7 - 8) * 9 = 100
1 + 2 * 3 / 4 * (56 - 7 + 8 + 9) = 100
1 - 2 * 3 / 4 * (5 - 6 + 7 - 8 * 9) = 100
1 - 2 * 3 / 4 * (5 * 6 - 7 - 89) = 100
1 - 2 * 3 * (45 / 6 - 7 - 8 - 9) = 100
1 + 2 * 3 * (45 / 6 - (7 - 8) * 9) = 100
1 + ((2 / 3 - 4 + 5) * 6 - 7 + 8) * 9 = 100
1 + ((2 / 3 - 4) / 5 * 6 + 7 + 8) * 9 = 100
1 * (2 / 3 + 4 * (5 / 6 + 7 + 8 + 9)) = 100
1 + 2 * (345 / 6 - 7 + 8 - 9) = 100
1 * (2 + (3 * 4 - 5) * (6 + 7 - 8 + 9)) = 100
1 * (2 - 3 * 4 * (5 / 6 + (7 - 8) * 9)) = 100
1 - 2 * ((3 / 4 - 5) * 6 - 7 - 8 - 9) = 100
1 - 2 * (3 + 45 / 6 * 7 * (8 - 9)) = 100
1 - 2 * (3 + 45 / 6 * 7 / (8 - 9)) = 100
# -*- coding:utf-8 -*-
import sys
import re
p = re.compile(r'(\d+)')
n = 0
fail = 0
exprs = set()
print "start testing..."
for line in sys.stdin:
a = line.split(' = ')
exp = p.sub(r'\1.0', a[0])
ans = float(a[1])
try:
ans_ = round(eval(exp), 7)
except:
print exp
if ans_ != ans: #式チェック
print "[%d] NG %s is not %d but %f" % (n, exp, ans, ans_)
fail += 1
if exp in exprs: #重複チェック
print "[%d] NG %s is already checked" % (n, exp)
exprs.add(exp)
n += 1
print "checked %d expressions, %d fails" % (n, fail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment