Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
for competitive programming
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
// "regexp"
)
type Pair struct {
p1, p2 interface{}
}
const (
initialBufSize = 1e4
maxBufSize = 1e8
)
var (
scanner = bufio.NewScanner(os.Stdin)
writer = bufio.NewWriter(os.Stdout)
)
func main() {
buf := make([]byte, initialBufSize)
scanner.Buffer(buf, maxBufSize)
scanner.Split(bufio.ScanWords)
// TODO:処理
fmt.Println(readFloat())
}
/*==========================================
* Library
*==========================================*/
func write(s string) {
writer.WriteString(s)
}
func print() {
writer.Flush()
}
// scanner.Split(bufio.ScanWords) をコメントアウトしないと使用不可
func readLine() (s string) {
if scanner.Scan() {
s = scanner.Text()
}
return s
}
func readInt() (read int) {
scanner.Scan()
read, err := strconv.Atoi(scanner.Text())
if err != nil {
panic(err)
}
return
}
func readFloat() (read float64) {
scanner.Scan()
read, err := strconv.ParseFloat(scanner.Text(), 64)
if err != nil {
panic(err)
}
return
}
func readRunes() (read []rune) {
scanner.Scan()
for _, v := range scanner.Text() {
read = append(read, v)
}
return
}
func readString() (read string) {
scanner.Scan()
read = scanner.Text()
return
}
func readStrings() (read []string) {
scanner.Scan()
for _, v := range scanner.Text() {
read = append(read, string(v))
}
return
}
func s2i(s string) int {
var intVal, e = strconv.Atoi(s)
if e != nil {
panic(e)
}
return intVal
}
func i2s(i int) string {
var strVal = strconv.Itoa(i)
return strVal
}
func s2f(s string) float64 {
var floatVal, e = strconv.ParseFloat(s, 64)
if e != nil {
panic(e)
}
return floatVal
}
func sum(i []int) int {
sum := 0
for _, val := range i {
sum += val
}
return sum
}
func split(s string) []string {
return strings.Fields(s)
}
func strAry2intAry(strs []string) []int {
var ret = make([]int, 0, len(strs))
for _, str := range strs {
var intVal, e = strconv.Atoi(string(str))
if e != nil {
panic(e)
}
ret = append(ret, intVal)
}
return ret
}
func intAry2strAry(nums []int) []string {
var ret = make([]string, 0, len(nums))
for _, num := range nums {
var strVal = strconv.Itoa(num)
ret = append(ret, strVal)
}
return ret
}
func ary2str(strs []string) string {
return strings.Join(strs, " ")
}
func reverse(res []int) []int {
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
res[i], res[j] = res[j], res[i]
}
return res
}
func initalize(res []int, init int) {
if len(res) == 0 {
return
}
res[0] = init
for i := 0; i < len(res); i++ {
copy(res[i:], res[:i])
}
}
//
// func regexpExample() {
// // Your code here!
// var str = "13:20"
// r := regexp.MustCompile(`(\d+):(\d+)`)
// fmt.Println(r.FindStringSubmatch(str))
// }
// type Country struct {
// gold int
// silver int
// blonze int
// }
// // 複数ソートのサンプル
// func stableSortExample() []Country{
// var country = []Country {
// {gold:1, silver:2, blonze:3},
// {gold:1, silver:2, blonze:3},
// {gold:1, silver:3, blonze:2},
// {gold:1, silver:3, blonze:3},
// }
// sort.SliceStable(country, func(i, j int) bool { return country[i].blonze > country[j].blonze })
// sort.SliceStable(country, func(i, j int) bool { return country[i].silver > country[j].silver })
// sort.SliceStable(country, func(i, j int) bool { return country[i].gold > country[j].gold })
// return country
// }
@horibahiroto
Copy link

horibahiroto commented Dec 17, 2019

func splitc(s string) []string {
    return strings.Split(s, ",")
}

@horibahiroto
Copy link

horibahiroto commented Dec 17, 2019

func Bubble(tgt []int) []int {
	loop := 0
	for j := len(tgt) - 1; j >= 0; j-- {
		for i := 0; i <= j-1; i++ {
			if tgt[i] > tgt[i+1] {
				tgt[i], tgt[i+1] = tgt[i+1], tgt[i]
			}
		}
	}
	return tgt
}

@horibahiroto
Copy link

horibahiroto commented Dec 17, 2019

@horibahiroto
Copy link

horibahiroto commented Dec 19, 2019

s2f がほしい

@horibahiroto
Copy link

horibahiroto commented Dec 27, 2019

インターフェースとして、sortKeyを指定して構造体の中身を第一ソート, 第二ソートみたいにできるようなものがほしい

@horibahiroto
Copy link

horibahiroto commented Dec 27, 2019

インターフェースとして、sortKeyを指定して構造体の中身を第一ソート, 第二ソートみたいにできるようなものがほしい

リフレクションとか使うのかな

@horibahiroto
Copy link

horibahiroto commented Dec 27, 2019

func reverse(res []int) []int {
    for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
        res[i], res[j] = res[j], res[i]
    }
    return res
}

@horibahiroto
Copy link

horibahiroto commented Feb 5, 2020

/** 各桁の和を求める */
func findSumOfDigits(n int) (sum int) {
	for n > 0 {
		sum += n % 10
		n /= 10
	}
	return
}

@horibahiroto
Copy link

horibahiroto commented Feb 18, 2020

func min(i []int) (min int) {
	for idx, val := range i {
		if idx == 0 {
			min = val
		}
		if min > val {
			min = val
		}
	}
	return
}

func max(i []int) (max int) {
	for idx, val := range i {
		if idx == 0 {
			max = val
		}
		if max < val {
			max = val
		}
	}
	return
}

@horibahiroto
Copy link

horibahiroto commented Mar 10, 2020

type Dice struct {
	top, bottom, left, right, front, back int
}

func (d *Dice) Up() {
	d.top, d.bottom, d.left, d.right, d.front, d.back =
		d.front, d.back, d.left, d.right, d.bottom, d.top
}
func (d *Dice) Down() {
	d.top, d.bottom, d.left, d.right, d.front, d.back =
		d.back, d.front, d.left, d.right, d.top, d.bottom
}
func (d *Dice) Left() {
	d.top, d.bottom, d.left, d.right, d.front, d.back =
		d.right, d.left, d.top, d.bottom, d.front, d.back
}
func (d *Dice) Right() {
	d.top, d.bottom, d.left, d.right, d.front, d.back =
		d.left, d.right, d.bottom, d.top, d.front, d.back
}
func (d *Dice) TurnOver() {
	d.Up()
	d.Up()
}
func (d *Dice) viewLeft() {
	d.top, d.bottom, d.left, d.right, d.front, d.back =
		d.top, d.bottom, d.front, d.back, d.right, d.left
}
func (d *Dice) viewRight() {
	d.top, d.bottom, d.left, d.right, d.front, d.back =
		d.top, d.bottom, d.back, d.front, d.left, d.right
}
func (a *Dice) clone() *Dice {
	copy := *a
	return &copy
}
func (d *Dice) equal(tgt *Dice) bool {
	fmt.Println(d, tgt)
	return d.top == tgt.top &&
		d.bottom == tgt.bottom &&
		d.left == tgt.left &&
		d.right == tgt.right &&
		d.front == tgt.front &&
		d.back == tgt.back
}

@horibahiroto
Copy link

horibahiroto commented Apr 24, 2020

package main

import (
	"bufio"
	"math"
	"os"
	"reflect"
	"sort"
	"strconv"
	"strings"
	// "regexp"
)

/* sort */
type pair struct {
	index  int64
	p1, p2 interface{}
}
type pairs []pair

func (slice pairs) Len() int {
	return len(slice)
}

func (slice pairs) Less(i, j int) bool {
	return slice[i].index < slice[j].index
}

func (slice pairs) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}

/* sort */
type Int64Slice []int64

func (slice Int64Slice) Len() int {
	return len(slice)
}

func (slice Int64Slice) Less(i, j int) bool {
	return slice[i] < slice[j]
}

func (slice Int64Slice) Swap(i, j int) {
	slice[i], slice[j] = slice[j], slice[i]
}
func Int64s(a []int64)                    { sort.Sort(Int64Slice(a)) }
func Int64sSliceAreSorted(a []int64) bool { return sort.IsSorted(Int64Slice(a)) }

const (
	initialBufSize = 1e4
	maxBufSize     = 1e8
	INF            = 1e8
)

var (
	scanner = bufio.NewScanner(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	di      = []int64{-1, 0, 1, 0}
	dj      = []int64{0, -1, 0, 1}
)

func main() {
	buf := make([]byte, initialBufSize)
	scanner.Buffer(buf, maxBufSize)
	scanner.Split(bufio.ScanWords)
	H, W := readInt(), readInt()
	grid := make([][]string, H)
	for i := int64(0); i < H; i++ {
		grid[i] = make([]string, W)
		grid[i] = readStrings()
	}
	//row check
	for i := int64(0); i < H; i++ {

	}
}

func checkRow(grid [][]string, W int64, rowNum int64) bool {
	check := true

	for i := int64(0); i < W; i++ {
		if grid[rowNum][i] != "." {
			check = false
			break
		}
	}
	return check
}
func checkColumn(grid [][]string, H int64, colNum int64) bool {
	check := true

	for i := int64(0); i < H; i++ {
		if grid[i][colNum] != "." {
			check = false
			break
		}
	}
	return check
}

/*==========================================
 *             Library
 *==========================================*/
func NextPermutation(x sort.Interface) bool {
	n := x.Len() - 1
	if n < 1 {
		return false
	}
	j := n - 1
	for ; !x.Less(j, j+1); j-- {
		if j == 0 {
			return false
		}
	}
	l := n
	for !x.Less(j, l) {
		l--
	}
	x.Swap(j, l)
	for k, l := j+1, n; k < l; {
		x.Swap(k, l)
		k++
		l--
	}
	return true
}
func doubleAry(h int64, w int64, init int64) (res [][]int64) {
	res = make([][]int64, h)
	for i := int64(0); i < h; i++ {
		res[i] = make([]int64, w)
		for j := int64(0); j < w; j++ {
			res[i][j] = init
		}
	}
	return
}

func aryEq(a []int64, b []int64) bool {
	return reflect.DeepEqual(a, b)
}

func clone(n []int64) (r []int64) {
	r = make([]int64, len(n))
	for i := 0; i < len(n); i++ {
		r[i] = n[i]
	}
	return
}
func write(s string) {
	writer.WriteString(s)
}

func print() {
	writer.Flush()
}

// scanner.Split(bufio.ScanWords) をコメントアウトしないと使用不可
func readLine() (s string) {
	if scanner.Scan() {
		s = scanner.Text()
	}
	return s
}

func readInt() (read int64) {
	scanner.Scan()
	read, err := strconv.ParseInt(scanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return
}

func readFloat() (read float64) {
	scanner.Scan()
	read, err := strconv.ParseFloat(scanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return
}

func readRunes() (read []rune) {
	scanner.Scan()
	for _, v := range scanner.Text() {
		read = append(read, v)
	}
	return
}

func readString() (read string) {
	scanner.Scan()
	read = scanner.Text()
	return
}

func readStrings() (read []string) {
	scanner.Scan()
	for _, v := range scanner.Text() {
		read = append(read, string(v))
	}
	return
}

func s2i(s string) int64 {
	var intVal, e = strconv.ParseInt(s, 10, 64)
	if e != nil {
		panic(e)
	}
	return intVal
}

func i2s(i int64) string {
	var strVal = strconv.FormatInt(i, 10)
	return strVal
}

func s2f(s string) float64 {
	var floatVal, e = strconv.ParseFloat(s, 64)
	if e != nil {
		panic(e)
	}
	return floatVal
}

func abs(i int64) int64 {
	return int64(math.Abs(float64(i)))
}

func max(a ...int64) int64 {
	ans := int64(0)
	for i, v := range a {
		if i == 0 {
			ans = v
		}
		if ans < v {
			ans = v
		}
	}
	return ans
}
func min(a ...int64) int64 {
	ans := int64(0)
	for i, v := range a {
		if i == 0 {
			ans = v
		}
		if ans > v {
			ans = v
		}
	}
	return ans
}

func sum(i []int64) int64 {
	sum := int64(0)
	for _, val := range i {
		sum += val
	}
	return sum
}

func split(s string) []string {
	return strings.Fields(s)
}

func strAry2intAry(strs []string) []int64 {
	var ret = make([]int64, 0, len(strs))
	for _, str := range strs {
		var intVal = s2i(str)
		ret = append(ret, intVal)
	}
	return ret
}

func intAry2strAry(nums []int64) []string {
	var ret = make([]string, 0, len(nums))
	for _, num := range nums {
		var strVal = i2s(num)
		ret = append(ret, strVal)
	}
	return ret
}

func ary2str(strs []string) string {
	return strings.Join(strs, " ")
}

func reverse(res []int64) []int64 {
	for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
		res[i], res[j] = res[j], res[i]
	}
	return res
}
func reverseStr(res []string) []string {
	for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
		res[i], res[j] = res[j], res[i]
	}
	return res
}

func ini(res []int, init int) {
	if len(res) == 0 {
		return
	}
	res[0] = init
	for i := 0; i < len(res); i++ {
		copy(res[i:], res[:i])
	}
}

//
// func regexpExample() {
//     // Your code here!
//     var str = "13:20"
//     r := regexp.MustCompile(`(\d+):(\d+)`)
//     fmt.Println(r.FindStringSubmatch(str))
// }

// type Country struct {
//     gold int
//     silver int
//     blonze int
// }

// // 複数ソートのサンプル
// func stableSortExample() []Country{
//     var country = []Country {
//         {gold:1, silver:2, blonze:3},
//         {gold:1, silver:2, blonze:3},
//         {gold:1, silver:3, blonze:2},
//         {gold:1, silver:3, blonze:3},
//     }
//     sort.SliceStable(country, func(i, j int) bool { return country[i].blonze > country[j].blonze })
//     sort.SliceStable(country, func(i, j int) bool { return country[i].silver > country[j].silver })
//     sort.SliceStable(country, func(i, j int) bool { return country[i].gold > country[j].gold })
//     return country
// }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment