如何打印出与前一个数字不同的每个数字?

huangapple go评论84阅读模式
英文:

How to print each digit of the number different from the one that precedes it?

问题

例如,如果我有数字35565,输出结果是3565。

所以,我的代码片段获取了每个数字,但我不知道如何保留前一个数字以便与下一个数字进行比较。

for {
    num = num / 10
    fmt.Print(num)
    
    if num/10 == 0 {
        break
    }
}
英文:

For example, if I have the number 35565, the output is 3565.

So, my snippet gets single digits, but I don't know how to keep previous digit to check it with the next one.

for {
num = num / 10
fmt.Print(num)
		
if num/10 == 0 {
	break
}
}

答案1

得分: 0

这种方法从右到左将数字分解为其各个位数,并将它们存储为整数切片,然后从左到右迭代这些位数,以构建具有“顺序唯一”位数的数字。

我最初尝试从左到右分解数字,但无法解决如何处理占位零;从右到左分解,我知道如何捕获这些零。

// unique 从非负数 x 中移除重复数字序列,
// 仅返回“顺序唯一”的数字:
// 12→12, 122→12, 1001→101, 35565→3565.
//
// 负数 x 返回 -1。
func unique(x int) int {
	switch {
	case x < 0:
		return -1
	case x <= 10:
		return x
	}

	// -- 将 x 分解为其各个位数
	var (
		mag     int   // x 的数量级
		nDigits int   // x 的位数
		digits  []int // x 的各个位数
	)

	mag = int(math.Floor(math.Log10(float64(x))))
	nDigits = mag + 1

	// 从右到左工作以保留占位零
	digits = make([]int, nDigits)
	for i := nDigits - 1; i >= 0; i-- {
		digits[i] = x % 10
		x /= 10
	}

	// -- 从左到右构建新的“顺序唯一”x
	var prevDigit, newX int

	for _, digit := range digits {
		if digit != prevDigit {
			newX = newX*10 + digit
		}
		prevDigit = digit
	}

	return newX
}

这里有一个带有测试的Go Playground

可以通过在开头翻转负号并在结尾恢复来适应处理负数。

英文:

This approach breaks down a number into its digits from right-to-left, storing them as a slice of ints, then iterates those digits from left-to-right to build up the number with "sequentially unique" digits.

I originally tried to break down the number from left-to-right but couldn't figure out how to handle place-holding zeroes; breaking it down from right-to-left, I know how to capture those zeroes.

// unique removes sequences of repeated digits from non-negative x,
// returning only &quot;sequentially unique&quot; digits:
// 12→12, 122→12, 1001→101, 35565→3565.
//
// Negative x yields -1.
func unique(x int) int {
	switch {
	case x &lt; 0:
		return -1
	case x &lt;= 10:
		return x
	}

	// -- Split x into its digits
	var (
		mag     int   // the magnitude of x
		nDigits int   // the number of digits in x
		digits  []int // the digits of x
	)

	mag = int(math.Floor(math.Log10(float64(x))))
	nDigits = mag + 1

	// work from right-to-left to preserve place-holding zeroes
	digits = make([]int, nDigits)
	for i := nDigits - 1; i &gt;= 0; i-- {
		digits[i] = x % 10
		x /= 10
	}

	// -- Build new, &quot;sequentially unique&quot;, x from left-to-right
	var prevDigit, newX int

	for _, digit := range digits {
		if digit != prevDigit {
			newX = newX*10 + digit
		}
		prevDigit = digit
	}

	return newX
}

Here's a Go Playground with a test.

This could be adapted to deal with negative numbers by flipping the negative sign at the beginning and restoring it at the end.

huangapple
  • 本文由 发表于 2023年1月5日 19:14:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75017571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定