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

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

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

问题

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

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

  1. for {
  2. num = num / 10
  3. fmt.Print(num)
  4. if num/10 == 0 {
  5. break
  6. }
  7. }
英文:

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.

  1. for {
  2. num = num / 10
  3. fmt.Print(num)
  4. if num/10 == 0 {
  5. break
  6. }
  7. }

答案1

得分: 0

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

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

  1. // unique 从非负数 x 中移除重复数字序列,
  2. // 仅返回“顺序唯一”的数字:
  3. // 12→12, 122→12, 1001→101, 35565→3565.
  4. //
  5. // 负数 x 返回 -1。
  6. func unique(x int) int {
  7. switch {
  8. case x < 0:
  9. return -1
  10. case x <= 10:
  11. return x
  12. }
  13. // -- 将 x 分解为其各个位数
  14. var (
  15. mag int // x 的数量级
  16. nDigits int // x 的位数
  17. digits []int // x 的各个位数
  18. )
  19. mag = int(math.Floor(math.Log10(float64(x))))
  20. nDigits = mag + 1
  21. // 从右到左工作以保留占位零
  22. digits = make([]int, nDigits)
  23. for i := nDigits - 1; i >= 0; i-- {
  24. digits[i] = x % 10
  25. x /= 10
  26. }
  27. // -- 从左到右构建新的“顺序唯一”x
  28. var prevDigit, newX int
  29. for _, digit := range digits {
  30. if digit != prevDigit {
  31. newX = newX*10 + digit
  32. }
  33. prevDigit = digit
  34. }
  35. return newX
  36. }

这里有一个带有测试的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.

  1. // unique removes sequences of repeated digits from non-negative x,
  2. // returning only &quot;sequentially unique&quot; digits:
  3. // 12→12, 122→12, 1001→101, 35565→3565.
  4. //
  5. // Negative x yields -1.
  6. func unique(x int) int {
  7. switch {
  8. case x &lt; 0:
  9. return -1
  10. case x &lt;= 10:
  11. return x
  12. }
  13. // -- Split x into its digits
  14. var (
  15. mag int // the magnitude of x
  16. nDigits int // the number of digits in x
  17. digits []int // the digits of x
  18. )
  19. mag = int(math.Floor(math.Log10(float64(x))))
  20. nDigits = mag + 1
  21. // work from right-to-left to preserve place-holding zeroes
  22. digits = make([]int, nDigits)
  23. for i := nDigits - 1; i &gt;= 0; i-- {
  24. digits[i] = x % 10
  25. x /= 10
  26. }
  27. // -- Build new, &quot;sequentially unique&quot;, x from left-to-right
  28. var prevDigit, newX int
  29. for _, digit := range digits {
  30. if digit != prevDigit {
  31. newX = newX*10 + digit
  32. }
  33. prevDigit = digit
  34. }
  35. return newX
  36. }

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:

确定