Golang Scan示例

说明

golang scan示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: fmt

示例#1
文件: is_fibo.go 项目: mewmew/playground

func main() {
	// Parse input.
	var count int
	_, err := fmt.Scan(&count)
	if err != nil {
		log.Fatalln(err)
	}
	ns := make([]int, count)
	for i := 0; i < count; i++ {
		_, err = fmt.Scan(&ns[i])
		if err != nil {
			log.Fatalln(err)
		}
	}

	// Check which input numbers that are fibonacci numbers.
	fib := NewFib()
	for _, n := range ns {
		if fib.IsFib(n) {
			fmt.Println("IsFibo")
		} else {
			fmt.Println("IsNotFibo")
		}
	}
}

示例#2
文件: sum.go 项目: EagleChen/timus

func main() {
	var N int
	fmt.Scan(&N)

	arr := make([][]int, N)
	for i := 0; i < N; i++ {
		arr[i] = make([]int, N)
		for j := 0; j < N; j++ {
			fmt.Scan(&arr[i][j])
		}
	}

	sum := make([]int, N)
	max := -2147483648
	for i := 0; i < N; i++ {
		copy(sum, arr[i])
		result := maxSum(sum)
		if result > max {
			max = result
		}
		for j := i + 1; j < N; j++ {
			for k := 0; k < N; k++ {
				sum[k] += arr[j][k]
			}
			result := maxSum(sum)
			if result > max {
				max = result
			}
		}
	}

	fmt.Println(max)
}

示例#3
文件: training.go 项目: mellowdrifter/codingame.com

func main() {
	for {
		// enemy1: name of enemy 1
		var enemy1 string
		fmt.Scan(&enemy1)

		// dist1: distance to enemy 1
		var dist1 int
		fmt.Scan(&dist1)

		// enemy2: name of enemy 2
		var enemy2 string
		fmt.Scan(&enemy2)

		// dist2: distance to enemy 2
		var dist2 int
		fmt.Scan(&dist2)

		// fmt.Fprintln(os.Stderr, "Debug messages...")

		if dist1 < dist2 {
			fmt.Println(enemy1)
		} else {
			fmt.Println(enemy2)
		}
	}
}

示例#4
文件: 2023._Donald_is_a_postman.go 项目: gopherboy/online_judge_solutions

func main() {
	var a int
	var b [1000]string
	fmt.Scan(&a)
	for cnt := a; cnt > 0; cnt-- {
		fmt.Scan(&b[cnt-1])
	}
	current := 1
	step := 0
	for cnt := a - 1; cnt >= 0; cnt-- {
		if b[cnt][0] == 'A' || b[cnt][0] == 'P' || b[cnt][0] == 'O' || b[cnt][0] == 'R' {
			if current == 2 {
				step += 1
			} else if current == 3 {
				step += 2
			}
			current = 1
		} else if b[cnt][0] == 'S' || b[cnt][0] == 'M' || b[cnt][0] == 'B' {
			if current != 2 {
				step++
			}
			current = 2
		} else {
			if current == 2 {
				step += 1
			} else if current == 1 {
				step += 2
			}
			current = 3
		}

	}
	fmt.Println(step)
}

示例#5
文件: solution.go 项目: esix/competitive-programming

func main() {
	var n, a, b int
	fmt.Scan(&n)
	fmt.Scan(&a)
	fmt.Scan(&b)
	fmt.Println(2 * n * a * b)
}

示例#6
文件: whirlybird.go 项目: kseals281/beginnings

func GetNumberSelection(use_even_numbers bool) int {
	var number string
	if use_even_numbers == true {
		evenNumbers := []string{"2", "4", "6", "8"}
		fmt.Println("\nPlease pick a number", evenNumbers)
		fmt.Scan(&number)
		for {
			for i := range evenNumbers {
				if evenNumbers[i] == number {
					return (i * 2) + 2
				}
			}
			fmt.Println("Your input was invalid. Please pick a number", evenNumbers)
			fmt.Scan(&number)
		}
	} else {
		oddNumbers := []string{"1", "3", "5", "7"}
		fmt.Println("\nPlease pick a number", oddNumbers)
		fmt.Scan(&number)
		for {
			for i := range oddNumbers {
				if oddNumbers[i] == number {
					return (i * 2) + 1
				}
			}
			fmt.Println("Your input was invalid. Please pick a number", oddNumbers)
			fmt.Scan(&number)
		}
	}
}

示例#7
文件: diagonal_difference.go 项目: writecoffee/random_practice

func main() {
	var n int
	fmt.Scan(&n)

	var v [][]int = make([][]int, n)
	for i := range v {
		v[i] = make([]int, n)
	}

	for i := 0; i < n*n; i++ {
		var number int
		fmt.Scan(&number)

		v[i/n][i%n] = number
	}

	var ldSum, rdSum int
	ldSum = 0
	rdSum = 0
	for i := 0; i < n; i++ {
		ldSum += v[i][i]
		rdSum += v[i][n-1-i]
	}

	fmt.Println(math.Abs(float64(ldSum - rdSum)))
}

示例#8
文件: codingame-Winamax_WAR.go 项目: israelchen/CodinGame

func main() {

	var n int
	fmt.Scan(&n)

	cardsp1 = make([]string, n)

	for i := 0; i < n; i++ {

		var cardp1 string
		fmt.Scan(&cardp1)

		cardsp1[i] = cardp1
	}

	var m int
	fmt.Scan(&m)

	cardsp2 = make([]string, m)

	for i := 0; i < m; i++ {

		var cardp2 string
		fmt.Scan(&cardp2)

		cardsp2[i] = cardp2
	}

	winner := "PAT"

	battlesFought = 0
	i = 0

	for {

		result := battle()
		fmt.Fprintf(os.Stderr, "battle %d result: %s\n", battlesFought, battleResultStrings[result])

		if result == battle_p2_out_of_cards {
			winner = "1"
			break
		}

		if result == battle_p1_out_of_cards {
			winner = "2"
			break
		}

		if result == battle_draw {
			winner = "PAT"
			break
		}
	}

	if winner == "PAT" {
		fmt.Println("PAT")
	} else {
		fmt.Printf("%s %d\n", winner, battlesFought)
	}
}

示例#9
文件: field.go 项目: goatcms/goat

func (f *Field) Scan() error {
	var attrs string
	var err error
	fmt.Print(correctTypes, "?: ")
	for true {
		fmt.Scanln(&f.Type)
		f.Type = strings.ToLower(f.Type)
		if varutil.IsArrContainStr(correctTypes, f.Type) {
			break
		}
		fmt.Print(correctTypes, "?: ")
	}
	fmt.Print("Min value of field: ")
	fmt.Scan(&f.Min)
	fmt.Print("Max value of field: ")
	fmt.Scan(&f.Max)

	var isAttrs string
	fmt.Print("Do you want add attribiutes? ")
	fmt.Scan(&isAttrs)
	isAttrs = strings.ToLower(isAttrs)
	if isAttrs == "yes" || isAttrs == "y" {
		fmt.Print("Insert field attrs (separated with space): ")
		fmt.Scan(&attrs)
		f.Attrs, err = varutil.SplitWhite(attrs)
		if err != nil {
			return err
		}
	}
	return nil
}

示例#10
文件: 1333.go 项目: ferhatelmas/algo

func main() {
	var n int
	fmt.Scan(&n)
	x, y, r := make([]float64, n), make([]float64, n), make([]float64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&x[i])
		fmt.Scan(&y[i])
		fmt.Scan(&r[i])
		r[i] *= r[i]
	}
	var c int
	for i := 0; i < 1000; i++ {
		for j := 0; j < 1000; j++ {
			x1 := (float64(i) + 0.5) / 1000.0
			y1 := (float64(j) + 0.5) / 1000.0
			var k int
			for ; k < n && dist(x1, y1, x[k], y[k]) > r[k]; k++ {
			}
			if k < n {
				c++
			}
		}
	}
	fmt.Printf("%.6f\n", float64(c)*100.0/1000000)
}

示例#11
文件: solution.go 项目: sbani/hackerrank

func main() {
	var a, b string
	fmt.Scan(&a)
	fmt.Scan(&b)

	fmt.Println(getDeletes(a, b))
}

示例#12
文件: CF553A Kyoya and Colored Balls.go 项目: jefferyyuan/Cookie

func main() {
	var k int
	fmt.Scan(&k)
	c := make([]int64, k)
	for i := 0; i < k; i++ {
		fmt.Scan(&c[i])
		if i > 0 {
			c[i] += c[i-1]
		}
	}
	var m = c[k-1]
	fact := make([]int64, c[k-1])
	fact[0] = 1
	for i := int64(1); i < m; i++ {
		fact[i] = fact[i-1] * i % mod
	}
	ans := int64(1)
	var n int64
	for i := 1; i < k; i++ {
		n = c[i] - 1
		m = c[i] - c[i-1] - 1
		ans = ans * fact[n] % mod * fastPow(fact[m]*fact[n-m]%mod, mod-2) % mod
	}
	fmt.Println(ans)
}

示例#13
文件: repeater.go 项目: hyPiRion/contests

func main() {
	var T int
	fmt.Scan(&T)
	for t := 1; t <= T; t++ {
		var N int
		fmt.Scan(&N)
		words := make([]string, N)
		for i := 0; i < N; i++ {
			fmt.Scan(&words[i])
		}
		// dedupe check
		if !possible(words) {
			fmt.Printf("Case #%d: Fegla Won\n", t)
		} else {
			sum := 0
			groups := make([][]int, N)
			for i := 0; i < N; i++ {
				groups[i] = partition(words[i])
			}
			// only works for n = 2
			for i := 0; i < len(groups[0]); i++ {
				if groups[0][i] > groups[1][i] {
					sum += groups[0][i] - groups[1][i]
				} else if groups[1][i] > groups[0][i] {
					sum += groups[1][i] - groups[0][i]
				}
			}

			fmt.Printf("Case #%d: %d\n", t, sum)
		}
	}
}

示例#14
文件: 1036.go 项目: ferhatelmas/algo

func main() {
	var n, s int
	fmt.Scan(&n)
	fmt.Scan(&s)
	if s%2 == 1 {
		fmt.Println(0)
	} else {
		s /= 2
		dp := make([][]*big.Int, n+1, n+1)
		for i := 0; i <= n; i++ {
			dp[i] = make([]*big.Int, s+1, s+1)
			for j := 0; j <= s; j++ {
				dp[i][j] = &big.Int{}
			}
		}
		dp[0][0] = big.NewInt(1)
		for i := 1; i <= n; i++ {
			for j := 0; j <= s; j++ {
				for k := 0; k <= up(j); k++ {
					dp[i][j].Add(dp[i][j], dp[i-1][j-k])
				}
			}
		}
		fmt.Println(dp[n][s].Mul(dp[n][s], dp[n][s]))
	}
}

示例#15
文件: 02.go 项目: C02Q5Y7HFVH3/ds

func main() {
	size := 0
	fmt.Scan(&size)
	data := make([]int, size)
	isNegative := true
	for i := 0; i < size; i++ {
		fmt.Scan(&data[i])
		if data[i] >= 0 {
			isNegative = false
		}
	}

	thisSum, maxSum := 0, 0
	thisSub := make([]int, 0)
	maxSub := make([]int, 1)
	for _, v := range data {
		thisSum += v
		thisSub = append(thisSub, v)
		if thisSum > maxSum {
			maxSum = thisSum
			maxSub = append([]int{}, thisSub...)
		} else if thisSum < 0 {
			thisSum = 0
			thisSub = []int{}
		}
	}

	if isNegative {
		fmt.Println(0, data[0], data[size-1])
	} else {
		fmt.Println(maxSum, maxSub[0],
			maxSub[len(maxSub)-1])
	}
}

示例#16
文件: 501b.go 项目: Vicissitude47/codeforces-solutions

func main() {
	var n, q int
	d := make(map[string]string)

	fmt.Scan(&q)

	for i := 0; i < q; i++ {
		var oldH, newH string

		fmt.Scan(&oldH, &newH)

		origH, exists := d[oldH]

		if exists {
			d[newH] = origH
			delete(d, oldH)
		} else {
			d[newH] = oldH
			n++
		}
	}

	fmt.Println(n)

	for newH, oldH := range d {
		fmt.Println(oldH, newH)
	}
}

示例#17
文件: codingame-Horse_Racing_Duals.go 项目: israelchen/CodinGame

func main() {
	var N int
	fmt.Scan(&N)

	powers := make([]int, 0)

	for i := 0; i < N; i++ {
		var Pi int
		fmt.Scan(&Pi)

		if Pi > 0 {
			powers = append(powers, Pi)
			fmt.Fprintln(os.Stderr, Pi)
		}
	}

	fmt.Fprintln(os.Stderr, powers)

	sort.Ints(powers)

	minD := 99999999999

	for i := 1; i < len(powers); i++ {

		delta := int(math.Abs(float64(powers[i] - powers[i-1])))

		if delta < minD {
			minD = delta
		}
	}

	fmt.Fprintln(os.Stderr, powers)

	fmt.Println(minD) // Write answer to stdout
}

示例#18
文件: funny_string.go 项目: decebal/go-training

func main() {
	var t int
	var s []byte

	fmt.Scan(&t)
	if t < 1 || t > 100 {
		return
	}

	for i := 0; i < t; i++ {
		fmt.Scan(&s)
		var r []byte
		for j := len(s) - 1; j > -1; j-- {
			r = append(r, s[j])
		}
		var flag int

		for j := 1; j < len(s); j++ {
			if absDif(s[j], s[j-1]) != absDif(r[j], r[j-1]) {
				flag = 1
				fmt.Println("Not Funny")
				break
			}
		}
		if flag == 0 {
			fmt.Println("Funny")
		}
	}
}

示例#19
文件: PIGBANK.go 项目: ronaflx/OJs

func main() {
	var t int
	fmt.Scan(&t)
	for cas := 1; cas <= t; cas++ {
		var pig, all, n int
		fmt.Scan(&pig, &all, &n)
		all -= pig
		for i := 0; i < n; i++ {
			fmt.Scan(&v[i], &w[i])
		}
		for i := 1; i <= all; i++ {
			dp[i] = INF
		}
		for i := 0; i < n; i++ {
			for j := w[i]; j <= all; j++ {
				dp[j] = min(dp[j], dp[j-w[i]]+v[i])
			}
		}
		if dp[all] != INF {
			fmt.Printf("The minimum amount of money in the piggy-bank is %d.\n", dp[all])
		} else {
			fmt.Println("This is impossible.")
		}
	}
}

示例#20
文件: 1119.go 项目: ferhatelmas/algo

func main() {
	var n, m, k int
	var dp [2][1001]node
	fmt.Scan(&n)
	fmt.Scan(&m)
	fmt.Scan(&k)
	nodes := make(map[node]bool, k)
	for i := 0; i < k; i++ {
		var x node
		fmt.Scan(&x.a, &x.b)
		nodes[x] = true
	}
	for i := 0; i <= m; i++ {
		dp[0][i] = node{i, 0}
	}

	for i := 1; i <= n; i++ {
		x := i & 1
		dp[x][0] = node{i, 0}
		for j := 1; j <= m; j++ {
			dp[x][j] = node{1, 0}.add(dp[x^1][j].min(dp[x][j-1]))

			if nodes[node{i, j}] {
				dp[x][j] = dp[x][j].min(dp[x^1][j-1].add(node{0, 1}))
			}
		}
	}
	fmt.Printf("%.0f\n", 100*dp[n&1][m].res())
}

示例#21
文件: descent.go 项目: Renerte/CodinGameSolutions

func main() {
	for {
		var spaceX, spaceY int
		fmt.Scan(&spaceX, &spaceY)

		HM := 0 //highest mountain

		MH := make([]int, 8) //slice for mountains

		for i := 0; i < 8; i++ {
			// mountainH: represents the height of one mountain, from 9 to 0. Mountain heights are provided from left to right.
			var mountainH int
			fmt.Scan(&mountainH)
			MH[i] = mountainH
		}

		for i := 0; i < 8; i++ { //determine highest
			if MH[i] > MH[HM] {
				HM = i
			}
		}

		if spaceX == HM { //is above highest?
			fmt.Println("FIRE")
		} else {
			fmt.Println("HOLD")
		}

		// fmt.Fprintln(os.Stderr, "Debug messages...")

		//fmt.Println("HOLD") // either:  FIRE (ship is firing its phase cannons) or HOLD (ship is not firing).
	}
}

示例#22
文件: 1582.go 项目: ferhatelmas/algo

func main() {
	var k1, k2, k3 float64
	fmt.Scan(&k1)
	fmt.Scan(&k2)
	fmt.Scan(&k3)
	fmt.Println(math.Floor((1000 / (1/k1 + 1/k2 + 1/k3)) + .5))
}

示例#23
文件: onboarding.go 项目: Renerte/CodinGameSolutions

func main() {
	for {
		// enemy1: name of enemy 1
		var enemy1 string
		fmt.Scan(&enemy1)

		// dist1: distance to enemy 1
		var dist1 int
		fmt.Scan(&dist1)

		// enemy2: name of enemy 2
		var enemy2 string
		fmt.Scan(&enemy2)

		// dist2: distance to enemy 2
		var dist2 int
		fmt.Scan(&dist2)

		// fmt.Fprintln(os.Stderr, "Debug messages...")
		if dist1 > dist2 {
			fmt.Println(enemy2)
		} else {
			fmt.Println(enemy1)
		}
		//fmt.Println("name of the enemy") // You have to output a correct ship name to shoot ("Buzz", enemy1, enemy2, ...)
	}
}

示例#24
文件: pro321b.go 项目: hillboy/contest

func main() {
	fmt.Scan(&n, &m)
	var temp string
	for i := 0; i < n; i++ {
		fmt.Scan(&temp, &j)
		if temp == "ATK" {
			atk = append(atk, j)
		} else {
			def = append(def, j)
		}
	}
	for i := 0; i < m; i++ {
		fmt.Scan(&j)
		fox = append(fox, j)
	}
	sort.Ints(def)
	sort.Ints(atk)
	sort.Ints(fox)
	a := killAll()
	b := onlyatk()
	if a > b {
		fmt.Println(a)
	} else {
		fmt.Println(b)
	}
}

示例#25
文件: B.go 项目: Goryudyuma/go-work

func main() {
	var N int
	var T string
	S := "b"
	fmt.Scan(&N)
	fmt.Scan(&T)
	if N%2 == 0 {
		fmt.Println("-1")
	} else {
		for i := 1; utf8.RuneCountInString(S) != N; i++ {
			if i%3 == 1 {
				S = "a" + S + "c"
			} else if i%3 == 2 {
				S = "c" + S + "a"
			} else {
				S = "b" + S + "b"
			}
		}
		if T == S {
			fmt.Println(N / 2)
		} else {
			fmt.Println("-1")
		}
	}
}

示例#26
文件: 499b.go 项目: Vicissitude47/codeforces-solutions

func main() {
	var n, m int
	var a, b string
	d := make(map[string]string)

	fmt.Scan(&n, &m)

	for i := 0; i < m; i++ {
		fmt.Scan(&a, &b)
		if len(a) > len(b) {
			d[a] = b
		} else {
			d[a] = a
		}
	}

	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		fmt.Print(d[a])
		if i < n-1 {
			fmt.Print(" ")
		} else {
			fmt.Println()
		}
	}
}

示例#27
文件: report-2-1.go 项目: starVader/elastic-report-golang

func main() {
	var k Fpair
	var str DatabaseType
	fmt.Println("Select Database :->\n 1. For Elasticsearch\n", "2. For Dynamo\n", "3. For Mysql\n", "Enter choice")
	fmt.Scan(&str)
	// fmt.Println(reflect.TypeOf(str))
	fmt.Println("Enter the search Field")
	fmt.Scan(&k.Qkey)

	fmt.Println("Enter the search string")
	fmt.Scan(&k.Qvalue)

	q = Filter{filter: []Fpair{k}}
	fmt.Println(q.filter[0])
	client, err := str.GetClient()
	CheckError(err)
	//type assertion for getclient
	switch v := client.(type) {
	case *elastic.Client:
		fmt.Println("Calling With Elasticsearch Client")
		GetReportEL(v)
	default:
		fmt.Println("No such Client available", v)
	}
}

示例#28
文件: 2033.go 项目: ferhatelmas/algo

func main() {
	stats := make(map[string]*stat, 6)
	var name string
	var p int
	for i := 0; i < 6; i++ {
		fmt.Scan(&name)
		fmt.Scan(&name)
		if s, ok := stats[name]; !ok {
			s = &stat{count: 1}
			fmt.Scan(&s.price)
			stats[name] = s
		} else {
			fmt.Scan(&p)
			s.count++
			if p < s.price {
				s.price = p
			}
		}
	}
	name = ""
	var maxCount, minPrice int
	for n, s := range stats {
		if name == "" || s.count > maxCount || (s.count == maxCount && s.price < minPrice) {
			name = n
			minPrice = s.price
			maxCount = s.count
		}
	}
	fmt.Println(name)
}

示例#29
文件: client.go 项目: kylelemons/go-rpcgen

func main() {

	var x int32
	var y int32
	flag.Parse()
	conn, err := openTLSClient("127.0.0.1:8000")
	sum := addservice.NewAddServiceClient(conn)
	if err != nil {
		log.Fatalf("dial: %s", err)
	}

	for {
		fmt.Printf("Enter 2 numbers> ")
		fmt.Scan(&x)
		fmt.Scan(&y)
		fmt.Printf("Sending %d and %d\n", x, y)
		in := &addservice.AddMessage{X: &x, Y: &y}
		out := &addservice.SumMessage{}
		if err := sum.Add(in, out); err != nil {
			log.Fatalf("Add failed with: %s", err)
		}
		if out.Z == nil {
			log.Fatalf("Sum failed with no message returned")
		}
		fmt.Printf("Received %d\n\n", *out.Z)
	}
}

示例#30
文件: 544A.go 项目: stauntonknight/algorithm

func main() {
	var count int
	var s string
	fmt.Scan(&count)
	fmt.Scan(&s)
	var chars [26]bool
	var indexes []int
	if count == 1 {
		fmt.Println("YES")
		fmt.Println(s)
		return
	}
	for i := 0; i < len(s); i++ {
		if chars[s[i]-'a'] == false {
			chars[s[i]-'a'] = true
			indexes = append(indexes, i)
			if len(indexes) == count {
				break
			}
		}
	}
	if len(indexes) == count {
		indexes = append(indexes, len(s))
		fmt.Println("YES")
		for i := 0; i < count; i++ {
			fmt.Println(s[indexes[i]:indexes[i+1]])
		}

	} else {
		fmt.Println("NO")
	}
}

展开阅读全文