题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

package main

//建立一个map叫做command依次存放所有存在的命令组合,
//如果这个key存在 则重置为unknow 例"b a"第一次存放时command["b a"]="where to add"
//第二次在“backplane abort”也会存在"b a"命令,此时将command["b a"]="unknown command"
//读取屏幕输入以回车结束
import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	command := make(map[string]string)
	un := "unknown command"
	initMap("reset", "", "reset what", un, command)
	initMap("reset", "board", "board fault", un, command)
	initMap("board", "add", "where to add", un, command)
	initMap("board", "delete", "no board at all", un, command)
	initMap("reboot", "backplane", "impossible", un, command)
	initMap("backplane", "abort", "install first", un, command)

	sc := bufio.NewScanner(os.Stdin)
	for sc.Scan() {
		cmd := sc.Text()
        //判断cmd命令是否存在于map
		todo, ok := command[cmd]
		if ok {
			fmt.Println(todo)
		} else {
			fmt.Println(un)
		}
	}

}

func initMap(first string, second string, todo string, unknow string, comd map[string]string) {
	if len(second) == 0 {
		for i := 0; i < len(first); i++ {
			comd[first[:i+1]] = todo
		}
	} else {
		for i := 0; i < len(first); i++ {
			for j := 0; j < len(second); j++ {
				if _, ok := comd[first[:i+1]+" "+second[:j+1]]; !ok {
					comd[first[:i+1]+" "+second[:j+1]] = todo
				} else {
					comd[first[:i+1]+" "+second[:j+1]] = unknow
				}
			}
		}
	}
}


全部评论

相关推荐

09-13 17:25
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
10-09 17:17
已编辑
门头沟学院 Java
活泼的代码渣渣在泡池...:同学你好,我也是学院本,后天要面这个亚信科技,是实习,请问问题都啥样呀,我项目就做了网上的,这是第一次面试
投递多益网络等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务