当前位置: 首页>>代码示例>>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;未经允许,请勿转载。