P1765题解

文章目录
  1. 1. 思路
  2. 2. 代码

本文最后更新于 <span id="expire-date"></span> 天前,文中部分描述可能已经过时。

题面见 https://www.luogu.com.cn/problem/P1765

思路

这道题用go写其实蛮简单的,思路打表即可

难点就在于输入

由于正常的fmt.Scan或Scanf或Scanln都无法获取一整行的输入,所以这里使用了一个比较底层的方法来完成

代码如下

代码

// luogu-P1765
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	var a [11451-4]int
	a[int('a')] = 1
	a[int('b')] = 2
	a[int('c')] = 3
	a[int('d')] = 1
	a[int('e')] = 2
	a[int('f')] = 3
	a[int('g')] = 1
	a[int('h')] = 2
	a[int('i')] = 3
	a[int('j')] = 1
	a[int('k')] = 2
	a[int('l')] = 3
	a[int('m')] = 1
	a[int('n')] = 2
	a[int('o')] = 3
	a[int('p')] = 1
	a[int('q')] = 2
	a[int('r')] = 3
	a[int('s')] = 4
	a[int('t')] = 1
	a[int('u')] = 2
	a[int('v')] = 3
	a[int('w')] = 1
	a[int('x')] = 2
	a[int('y')] = 3
	a[int('z')] = 4
	a[int(' ')] = 1
	cnt := 0
	read := bufio.NewReader(os.Stdin)
	s, _ := read.ReadString('\n')//注意,这里函数的多返回值需要处理一下
	strings.Trim(s, "\n")//去掉字符串尾部的\n
	//fmt.Printf(s)
	for _, k := range s {
		cnt += a[int(k)-1]
	}
	fmt.Println(cnt)
}

本文作者:Caviar-X

本文链接: http://caviar-x.top/2021/05/slu-P1765/

评论