當前位置: 首頁>>代碼示例>>Golang>>正文


Golang fmt.Sscan函數代碼示例

本文整理匯總了Golang中fmt.Sscan函數的典型用法代碼示例。如果您正苦於以下問題:Golang Sscan函數的具體用法?Golang Sscan怎麽用?Golang Sscan使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Sscan函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: main

func main() {
	var n int
	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	for scanner.Scan() {
		t := strings.Split(scanner.Text(), ";")
		u := strings.Split(t[0], ",")
		fmt.Sscan(t[1], &n)
		v, vk := make(map[int]bool, len(u)), make([]int, len(u))
		for i := range u {
			var w int
			fmt.Sscan(u[i], &w)
			v[w], vk = true, append(vk, w)
		}
		f := false
		for _, i := range vk {
			if 2*i < n && v[n-i] {
				if f {
					fmt.Print(";")
				}
				fmt.Print(i, ",", n-i)
				f = true
			}
		}
		if !f {
			fmt.Print("NULL")
		}
		fmt.Println()
	}
}
開發者ID:oorja,項目名稱:ce-challenges,代碼行數:34,代碼來源:number_pairs.go

示例2: sLane

func sLane(in string) string {
	var n, t int
	_, err := fmt.Sscan(in, &n, &t)
	if err != nil {
		return ""
	}
	fmt.Printf("n = %d, t = %d\n", n, t)
	w := make([]int, n)
	for i := 0; i < n; i += 2 {
		var f int
		_, err := fmt.Sscan(in[i+3:], &f)
		if err != nil {
			return ""
		}
		w = append(w, f)
	}

	v := make(TestCases, t)
	for z := 0; z < t; z++ {
		var tc TestCase
		_, err := fmt.Sscan(in[z+(n*2)+2:], &tc.Entry, &tc.Exit)
		if err != nil {
			return ""
		}
		v[z] = tc
	}
	fmt.Printf("%#v\n", v)

	for z := 0; z < len(v); z++ {

	}
	return ""
}
開發者ID:JonPulfer,項目名稱:hackerrank,代碼行數:33,代碼來源:servicelane.go

示例3: intersect

func intersect(s, t string) string {
	var (
		a    int
		x, y []int
		r    []string
	)
	u, v := strings.Split(s, ","), strings.Split(t, ",")
	for _, i := range u {
		fmt.Sscan(i, &a)
		x = append(x, a)
	}
	for _, i := range v {
		fmt.Sscan(i, &a)
		y = append(y, a)
	}
	for i, j := 0, 0; i < len(u) && j < len(v); {
		if x[i] == y[j] {
			r = append(r, fmt.Sprint(x[i]))
			i++
			j++
		} else if x[i] > y[j] {
			j++
		} else {
			i++
		}
	}
	return strings.Join(r, ",")
}
開發者ID:nikai3d,項目名稱:ce-challenges,代碼行數:28,代碼來源:intersection.go

示例4: main

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	// width: the number of cells on the X axis
	var width int
	scanner.Scan()
	fmt.Sscan(scanner.Text(), &width)

	// height: the number of cells on the Y axis
	var height int
	scanner.Scan()
	fmt.Sscan(scanner.Text(), &height)

	node := make([][]bool, height) //slice for nodes' rows

	for i := 0; i < height; i++ {
		node[i] = make([]bool, width) //slice - nodes' row
	}

	for i := 0; i < height; i++ { //find nodes
		scanner.Scan()
		line := scanner.Text() // width characters, each either 0 or .
		for j := 0; j < len(line); j++ {
			if string(line[j]) == "0" {
				node[i][j] = true
			}
		}
	}

	for i := 0; i < height; i++ {
		for j := 0; j < width; j++ {
			if node[i][j] { //is there a node?
				res := strconv.Itoa(j) + " " + strconv.Itoa(i) + " " //start of response
				for k := j + 1; k < width; k++ {
					if node[i][k] { //is there a node?
						res += strconv.Itoa(k) + " " + strconv.Itoa(i) + " " //inner response
						break
					}
				}
				if len(res) < 7 { //was node found?
					res += "-1 -1 "
				}
				for k := i + 1; k < height; k++ {
					if node[k][j] { //is there a node?
						res += strconv.Itoa(j) + " " + strconv.Itoa(k) //end response
						break
					}
				}
				if len(res) < 11 { //was node found?
					res += "-1 -1"
				}
				fmt.Println(res) //output response
			}
		}
	}

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

	//fmt.Println("0 0 1 0 0 1") // Three coordinates: a node, its right neighbor, its bottom neighbor
}
開發者ID:Renerte,項目名稱:CodinGameSolutions,代碼行數:60,代碼來源:apu_init.go

示例5: main

func main() {
	B := new(big.Int).Exp(big.NewInt(2), big.NewInt(20), big.NewInt(0))

	p := new(big.Int)
	g := new(big.Int)
	h := new(big.Int)

	_, err := fmt.Sscan("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171", p)
	_, err = fmt.Sscan("11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568", g)
	_, err = fmt.Sscan("3239475104050450443565264378728065788649097520952449527834792452971981976143292558073856937958553180532878928001494706097394108577585732452307673444020333", h)
	if err != nil {
		fmt.Println(err)
	}

	// Create a map that uses the calculations for x1's as keys
	hashTable := make(map[string]int64, B.Int64())

	var i int64
	for i = 0; i < B.Int64(); i++ {
		hashTable[computeX1(p, g, h, big.NewInt(i)).String()] = i
	}

	// Calculate the x0's and check if they are part of the hash table
	for i = 0; i < B.Int64(); i++ {
		x0Res := computeX0(p, g, B, big.NewInt(i))

		x1, found := hashTable[x0Res.String()]
		if found {
			x := B.Mul(B, big.NewInt(i))
			x.Add(x, big.NewInt(x1))
			fmt.Println("Solution:", x)
			return
		}
	}
}
開發者ID:MariaRigaki,項目名稱:golang-stuff,代碼行數:35,代碼來源:week5.go

示例6: main

func main() {
	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	for scanner.Scan() {
		var n int
		fmt.Sscan(scanner.Text(), &n)
		b := make([]int, n)
		scanner.Scan()
		s := strings.Split(scanner.Text(), ",")
		fmt.Sscan(s[0], &b[0])
		for i := 1; i < n; i++ {
			var v int
			fmt.Sscan(s[i], &v)
			b[i] = b[i-1] + v
		}
		for i := 1; i < n; i++ {
			scanner.Scan()
			s = strings.Split(scanner.Text(), ",")
			var v int
			fmt.Sscan(s[0], &v)
			b[0] += v
			for j := 1; j < n; j++ {
				var v int
				fmt.Sscan(s[j], &v)
				b[j] = min(b[j], b[j-1]) + v
			}
		}
		fmt.Println(b[n-1])
	}
}
開發者ID:oorja,項目名稱:ce-challenges,代碼行數:34,代碼來源:min_path_sum.go

示例7: main

func main() {
	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	for scanner.Scan() {
		s := strings.Split(scanner.Text(), ";")
		t := strings.Fields(s[1])
		var m, n, c int
		fmt.Sscan(s[0], &n)
		u := make([]int, len(t))
		for ix, i := range t {
			fmt.Sscan(i, &u[ix])
		}
		for i := 0; i < n; i++ {
			c += u[i]
		}
		if c > m {
			m = c
		}
		for len(u) > n {
			c = c - u[0] + u[n]
			if c > m {
				m = c
			}
			u = u[1:]
		}
		fmt.Println(m)
	}
}
開發者ID:oorja,項目名稱:ce-challenges,代碼行數:32,代碼來源:max_range_sum.go

示例8: GetEnvs

// Convert and validate the GOAGAIN_FD, GOAGAIN_NAME, and GOAGAIN_PPID
// environment variables.  If all three are present and in order, this
// is a child process that may pick up where the parent left off.
func GetEnvs() (l *net.TCPListener, ppid int, err error) {
	var fd uintptr
	_, err = fmt.Sscan(os.Getenv("GOAGAIN_FD"), &fd)
	if nil != err {
		return
	}
	var i net.Listener
	i, err = net.FileListener(os.NewFile(fd, os.Getenv("GOAGAIN_NAME")))
	if nil != err {
		return
	}
	l = i.(*net.TCPListener)
	if err = syscall.Close(int(fd)); nil != err {
		return
	}
	_, err = fmt.Sscan(os.Getenv("GOAGAIN_PPID"), &ppid)
	if nil != err {
		return
	}
	if syscall.Getppid() != ppid {
		err = errors.New(fmt.Sprintf(
			"GOAGAIN_PPID is %d but parent is %d\n",
			ppid,
			syscall.Getppid(),
		))
		return
	}
	return
}
開發者ID:brunoqc,項目名稱:goagain,代碼行數:32,代碼來源:goagain.go

示例9: compareSpamSum

func compareSpamSum(left, right string) int {
	var leftSum, rightSum spamsum.SpamSum
	fmt.Sscan(left, &leftSum)
	fmt.Sscan(right, &rightSum)
	score := leftSum.Compare(rightSum)
	return int(score)
}
開發者ID:postfix,項目名稱:spamsum,代碼行數:7,代碼來源:from_git_repo.go

示例10: TestVoteBox

func TestVoteBox(t *testing.T) {
	score := c.NewScore(69)
	server := hm.NewMock(map[string]hm.Responder{
		"/v": hm.FuncResponder(func(c *hm.Context) hm.Response {
			var vote, lastVote int
			// get values from the query parameters
			fmt.Sscan(c.Request.URL.Query().Get("vote"), &vote)
			fmt.Sscan(c.Request.URL.Query().Get("lastvote"), &lastVote)

			score.UserVote(vote, lastVote)

			return hm.NewOKResponse(fmt.Sprint(score.Score))
		}),
	})

	fntest.NewDummyTestApp("/", server)

	votebox := &VoteBoxModel{}
	votebox.VoteUrl = "/v"
	votebox.Vote = score

	// Start testing
	server.Wait(func() { votebox.DoVote(1) }, 1)
	require.Equal(t, votebox.Vote.Score, 70)
	server.Wait(func() { votebox.DoVote(-1) }, 1)
	require.Equal(t, votebox.Vote.Score, 68)
	server.Wait(func() { votebox.DoVote(-1) }, 1)
	require.Equal(t, votebox.Vote.Score, 69)
}
開發者ID:phaikawl,項目名稱:wadereddi,代碼行數:29,代碼來源:client_test.go

示例11: Recover

// Recover from a seamless binary upgrade and use an already
// existing listener to take over the connections
func Recover() (l net.Listener, ppid int, err error) {
	var fd uintptr
	_, err = fmt.Sscan(os.Getenv("OLD_FD"), &fd)
	if err != nil {
		return
	}
	var i net.Listener
	i, err = net.FileListener(os.NewFile(fd, os.Getenv("OLD_NAME")))
	if err != nil {
		return
	}
	switch i.(type) {
	case *net.TCPListener:
		l = i.(*net.TCPListener)
	case *net.UnixListener:
		l = i.(*net.UnixListener)
	default:
		err = errors.New(fmt.Sprintf(
			"file descriptor is %T not *net.TCPListener or *net.UnixListener", i))
		return
	}
	if err = syscall.Close(int(fd)); err != nil {
		return
	}
	_, err = fmt.Sscan(os.Getenv("OLD_PPID"), &ppid)
	if err != nil {
		return
	}
	return
}
開發者ID:lu-zero,項目名稱:mirrorbits,代碼行數:32,代碼來源:process.go

示例12: Load

func (this *BoolOptions) Load() {
	file, err := os.Open("settings.txt")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer file.Close()

	v := reflect.ValueOf(this).Elem()
	var n int

	for err == nil {
		var key, value string
		n, err = fmt.Fscanf(file, "%s %s\n", &key, &value)
		if n == 0 {
			return
		}

		fieldValue := v.FieldByName(key)
		switch fieldValue.Kind() {
		case reflect.Bool:
			var b bool
			fmt.Sscan(value, &b)
			fieldValue.SetBool(b)
		case reflect.Float32, reflect.Float64:
			var f float64
			fmt.Sscan(value, &f)
			fieldValue.SetFloat(f)
		}
	}
}
開發者ID:krux02,項目名稱:turnt-octo-wallhack,代碼行數:31,代碼來源:Settings.go

示例13: CalculatePdpPrep

func (d *DatasourceDerive) CalculatePdpPrep(newValue string, interval float64) (float64, error) {
	if float64(d.Heartbeat) < interval {
		d.LastValue = Undefined
	}

	rate := math.NaN()
	newPdp := math.NaN()
	if newValue != Undefined && float64(d.Heartbeat) >= interval {
		newInt := new(big.Int)
		_, err := fmt.Sscan(newValue, newInt)
		if err != nil {
			return math.NaN(), errors.Errorf("not a simple signed integer: %s", newValue)
		}
		if d.LastValue != "U" {
			prevInt := new(big.Int)
			_, err := fmt.Sscan(d.LastValue, prevInt)
			if err != nil {
				return math.NaN(), errors.Wrap(err, 0)
			}
			diff := new(big.Int)
			diff.Sub(newInt, prevInt)

			newPdp = float64(diff.Uint64())
			rate = newPdp / interval
		}
	}

	if !d.checkRateBounds(rate) {
		newPdp = math.NaN()
	}

	d.LastValue = newValue

	return newPdp, nil
}
開發者ID:untoldwind,項目名稱:gorrd,代碼行數:35,代碼來源:datasource_derive.go

示例14: blockexplorerBlockDataHandler

// Handles the call to get many data points on blocks
func (srv *Server) blockexplorerBlockDataHandler(w http.ResponseWriter, req *http.Request) {
	// Extract the start and end point from the request
	var start, finish types.BlockHeight
	_, err := fmt.Sscan(req.FormValue("start"), &start)
	if err != nil {
		writeError(w, "Malformed or no start height", http.StatusBadRequest)
		return
	}

	// If a range end is not given, assume the range end to be one
	// greater than the range start, returning one block
	_, err = fmt.Sscan(req.FormValue("finish"), &finish)
	if err != nil {
		finish = start + 1
	}

	// Bounds checking is done inside BlockInfo
	blockSummaries, err := srv.blocke.BlockInfo(start, finish)
	if err != nil {
		writeError(w, err.Error(), http.StatusBadRequest)
		return
	}

	writeJSON(w, blockSummaries)
}
開發者ID:mm3,項目名稱:Sia,代碼行數:26,代碼來源:blockexplorer.go

示例15: main

func main() {
	sq := make(map[int]bool)
	for i := 0; i < 46341; i++ {
		sq[i*i] = true
	}

	data, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer data.Close()
	scanner := bufio.NewScanner(data)
	scanner.Scan()
	var n int
	fmt.Sscan(scanner.Text(), &n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		var x int
		fmt.Sscan(scanner.Text(), &x)
		num, l, bot, top := 0, make(map[int]bool), sqrt(x/2), sqrt(x)+1
		for j := bot; j < top; j++ {
			t := x - j*j
			if sq[t] && !l[t] {
				l[j*j], num = true, num+1
			}
		}
		fmt.Println(num)
	}
}
開發者ID:oorja,項目名稱:ce-challenges,代碼行數:29,代碼來源:double_squares.go


注:本文中的fmt.Sscan函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。