算法分析与设计

3,4,5组成的七位数,相邻数字不同的数的个数

F#

let t = [3;4;5]
seq{
    for a in t do
        for b in t do
            for c in t do
                for d in t do
                    for e in t do
                        for f in t do
                            for g in t do
                                yield a::b::c::d::e::f::g::[]
}
|> Seq.filter (fun x ->
    let rec j = function
        | a::b::_ when a = b -> false
        | _::_::[] -> true
        | _::b::tail -> j (b::tail)
    j x)
|> Seq.length
|> System.Console.WriteLine

JavaScript

Array(7)
        .fill([...Array(3).keys()].map(x => (x + 3).toString()))
        .reduce((a, b) => a.flatMap(x => b.map(y => x + y)))
        .filter(x => !/(\d)\1/.test(x))
        .map(Number)
        .length

小于200,除7余2,除8余3,除9余1

F#

seq { 7 * 8 - 5 .. 56 .. 200 }
|> Seq.filter (fun x -> x % 9 = 1)
|> Seq.iter System.Console.WriteLine

JavaScript

[...Array(200 - (7 * 8 - 5)).keys()].map(x=>x + (7 * 8 - 5)).filter((_, i) => i % 56 === 0).filter(x => x % 9 === 1)

画*

F#

open System
let n = Console.ReadLine() |> Convert.ToInt32
let nm = n / 2 + 1
[1..n] 
|> List.iter (fun x -> 
    Console.WriteLine(
        "".PadLeft(Math.Abs(nm - x))
        + "".PadLeft(2*(nm - Math.Abs(nm - x))-1, '*')))

JavaScript

require('readline').createInterface({
    input: process.stdin
}).on('line', (line) => {
    const nm = Math.floor(+line / 2 + 1);
    [...Array(+line + 1).keys()].slice(1).forEach(x => console.log(" ".repeat(Math.abs(nm - x)) + "*".repeat(2 * (nm - Math.abs(nm - x)) - 1)));
});

取石子游戏

F#

open System

let GoldenRatio = (sqrt 5. + 1.) / 2.

let WythoffsGame m n =
    let a::b::_ = List.sort [m; n]
    if (GoldenRatio * float (b - a)).ToString().Split('.').[0] |> Int32.Parse = a then false else true

[<EntryPoint>]
let main _ = 
    while true do 
        match Console.ReadLine() with
        | null -> exit 0
        | str ->
            let d = str.Split ' ' |> Array.map Int32.Parse
            WythoffsGame d.[0] d.[1] |> printfn "%A"
    0

JavaScript

require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', (line) => {
    const x = line.split(" ").map(Number).sort().reverse();
    console.log(parseInt(x.reduce((a, b) => a - b) * (Math.sqrt(5) + 1) / 2) !== x[0]);
});

蛇行矩阵

F#

  
open System

type Op = UpRight | DownLeft

let ZigZagMatrix m n =
    let map = Array2D.create m n 1
    let rec run x y i op =
        map.[x, y] <- i
        match op with
        | _        when x = m - 1 && y = n - 1 -> map
        | DownLeft when x = m - 1              -> run x       (y + 1) (i + 1) UpRight
        | UpRight  when y = n - 1              -> run (x + 1) y       (i + 1) DownLeft
        | DownLeft when y = 0                  -> run (x + 1) 0       (i + 1) UpRight
        | UpRight  when x = 0                  -> run 0       (y + 1) (i + 1) DownLeft
        | DownLeft                             -> run (x + 1) (y - 1) (i + 1) DownLeft
        | _                                    -> run (x - 1) (y + 1) (i + 1) UpRight 
    run 0 0 1 UpRight

let SnakeMatrix x = ZigZagMatrix x x

[<EntryPoint>]
let main _ = 
    while true do
        match Console.ReadLine() with
        | null -> exit 0
        | str ->
            let n = Int32.Parse str
            SnakeMatrix n
            |> Seq.cast<int>
            |> Seq.chunkBySize n
            |> Seq.map (Array.map Convert.ToString >> String.concat " ")
            |> Seq.iter (printfn "%s")
    0

JavaScript

require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
}).on('line', (line) => {
    const r = Array(2).fill([...Array(+line).keys()]).reduce((a, b) => a.flatMap(x => b.map(y => x + [y]))),
        s = [...r].sort((a, b) => {
            const f = x => [+x[0] + +x[1], (+x[0] + +x[1]) % 2 ? -x[1] : x[1]], x = f(a), y = f(b);
            return x[0] === y[0] ? x[1] - y[1] : x[0] - y[0];
        }),
        m = r.map(x => s.indexOf(x) + 1);
    [...Array(+line).keys()].forEach(i => console.log(m.slice(i * +line, i * +line + (+line - i)).join()));
});

由1,3,4,5,7,8这六个数字所组成的六位数中,能被11整除的最大的数是多少

JavaScript

Math.max.apply({},Array(6).fill([1,3,4,5,7,8]).reduce((a, b) => a.flatMap(x => b.map(y => x + [y]))).filter(x => !/(.).*?\1/.test(x)).filter(x => x%11==0))

请用1,2,5,7,8,9这六个数字来组成一个五位数,使得它能被75整除,并求出这样的五位数有几个

JavaScript

Array(5).fill([1,2,5,7,8,9].map(x => x.toString())).reduce((a, b) => a.flatMap(x => b.map(y => x + y))).filter(x => !/(.).*?\1/.test(x)).filter(x => x%75==0).length

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注