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'
}