4.12筆試題目

1).和三位數(shù)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).和三位數(shù)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).和三位數(shù)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).和三位數(shù)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'
}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容