4.12笔试题目

2024-04-11  本文已影响0人  成功的失败者

1).和三位数246相比,仅有1个号码相同,且位置相同。

func f1() {
    num := 246
    num1 := num % 10
    num2 := num / 10 % 10
    num3 := num / 100 % 10
    res := []int{}
    for i := 0; i < 10000; i++ {
        mod1 := i % 10
        mod2 := i / 10 % 10
        mod3 := i / 100 % 10
        count := 0
        if mod1 == num1 {
            count++
        }
        if mod2 == num2 {
            count++
        }
        if mod3 == num3 {
            count++
        }
        if count == 1 {
            res = append(res, i)
        }
    }
    fmt.Printf("%#v\n", res)
}

2).和三位数692相比,仅有2个号码相同,但位置都不相同。

func f2() {
    num := 692
    num1 := num % 10
    num2 := num / 10 % 10
    num3 := num / 100 % 10
    res := []int{}
    for i := 0; i < 10000; i++ {
        mod1 := i % 10
        mod2 := i / 10 % 10
        mod3 := i / 100 % 10
        count := 0
        if num1 == mod2 || num1 == mod3 {
            count++
        }
        if num2 == mod1 || num2 == mod3 {
            count++
        }
        if num3 == mod1 || num3 == mod2 {
            count++
        }
        if count == 2 {
            res = append(res, i)
        }
    }
    fmt.Printf("%#v\n", res)
}

3).和三位数174相比,没有一个号码相同。

func f3() {
    num := 174
    num1 := num % 10
    num2 := num / 10 % 10
    num3 := num / 100 % 10
    res := []int{}
    for i := 0; i < 10000; i++ {
        mod1 := i % 10
        mod2 := i / 10 % 10
        mod3 := i / 100 % 10
        count := 0
        if num1 == mod2 || num1 == mod3 || num1 == mod1 {
            count++
        }
        if num2 == mod2 || num2 == mod3 || num2 == mod1 {
            count++
        }
        if num3 == mod2 || num3 == mod3 || num3 == mod1 {
            count++
        }
        if count == 0 {
            res = append(res, i)
        }
    }
    fmt.Printf("%#v\n", res)
}

4).和三位数419相比,有1个号码相同,但位置不相同。

func f4() {
    num := 419
    num1 := num % 10
    num2 := num / 10 % 10
    num3 := num / 100 % 10
    res := []int{}
    for i := 0; i < 10000; i++ {
        mod1 := i % 10
        mod2 := i / 10 % 10
        mod3 := i / 100 % 10
        count := 0
        if num1 == mod2 || num1 == mod3 {
            count++
        }
        if num2 == mod1 || num2 == mod3 {
            count++
        }
        if num3 == mod1 || num3 == mod2 {
            count++
        }
        if count == 1 {
            res = append(res, i)
        }
    }
    fmt.Printf("%#v\n", res)
}

请写出一个程序,对以下bool表达式进行解析并运算出结果


func f5(str string) bool {
    statck := []rune{}
    for _, char := range str {
        if char == ',' {
            continue
        }
        if char != ')' {
            statck = append(statck, char)
            continue
        }
        t := 0
        f := 0
        for statck[len(statck)-1] != '(' {
            val := statck[len(statck)-1]
            statck = statck[:len(statck)-1]
            if val == 't' {
                t++
            } else {
                f++
            }
        }
        statck = statck[:len(statck)-1]
        op := statck[len(statck)-1]
        statck = statck[:len(statck)-1]
        char = 't'
        // 判断运算符
        switch op {
        case '|':
            if t == 0 {
                char = 'f'
            }
            statck = append(statck, char)
        case '!':
            if f != 1 {
                char = 'f'
            }
            statck = append(statck, char)
        case '&':
            if f != 0 {
                char = 'f'
            }
            statck = append(statck, char)
        }
    }
    return statck[len(statck)-1] == 't'
}

上一篇 下一篇

猜你喜欢

热点阅读