例如,如果我有數字 35565,則輸出為 3565。
所以,我的程式碼片段得到了個位數,但我不知道如何保留前一個數字以與下一個數字進行檢查。
for { num = num / 10 fmt.Print(num) if num/10 == 0 { break } }
正確答案
這種方法從右到左將數字分解為數字,將它們儲存為整數切片,然後從左到右迭代這些數字以建構具有“連續唯一”數字的數字。
我最初嘗試從左到右分解數字,但不知道如何處理佔位零;從右到左分解它,我知道如何捕獲這些零。
// unique removes sequences of repeated digits from non-negative x, // returning only "sequentially unique" digits: // 12→12, 122→12, 1001→101, 35565→3565. // // Negative x yields -1. func unique(x int) int { switch { case x < 0: return -1 case x <= 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 >= 0; i-- { digits[i] = x % 10 x /= 10 } // -- Build new, "sequentially unique", x from left-to-right var prevDigit, newX int for _, digit := range digits { if digit != prevDigit { newX = newX*10 + digit } prevDigit = digit } return newX }
這是一個go playground 進行測試。
可以透過翻轉開頭的負號並在末尾恢復它來適應處理負數。
以上是如何列印與前面的數字不同的數字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Golang和Python的主要區別在於並發模型、類型系統、性能和執行速度。 1.Golang使用CSP模型,適用於高並發任務;Python依賴多線程和GIL,適合I/O密集型任務。 2.Golang是靜態類型,Python是動態類型。 3.Golang編譯型語言執行速度快,Python解釋型語言開發速度快。

Golang通常比C 慢,但Golang在並發編程和開發效率上更具優勢:1)Golang的垃圾回收和並發模型使其在高並發場景下表現出色;2)C 通過手動內存管理和硬件優化獲得更高性能,但開發複雜度較高。

Golang在雲計算和DevOps中的應用廣泛,其優勢在於簡單性、高效性和並發編程能力。 1)在雲計算中,Golang通過goroutine和channel機制高效處理並發請求。 2)在DevOps中,Golang的快速編譯和跨平台特性使其成為自動化工具的首選。

Golang和C 在執行效率上的表現各有優勢。 1)Golang通過goroutine和垃圾回收提高效率,但可能引入暫停時間。 2)C 通過手動內存管理和優化實現高性能,但開發者需處理內存洩漏等問題。選擇時需考慮項目需求和團隊技術棧。

Golang更適合高並發任務,而Python在靈活性上更有優勢。 1.Golang通過goroutine和channel高效處理並發。 2.Python依賴threading和asyncio,受GIL影響,但提供多種並發方式。選擇應基於具體需求。

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

selectgolangforhighpperformanceandcorrency,ifealforBackendServicesSandNetwork程序; selectpypypythonforrapiddevelopment,dataScience和machinelearningDuetoitsverserverserverserversator versator anderticality andextility andextentensivelibraries。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。Golang以其并发模型和高效性能著称,Python则以简洁语法和丰富库生态系统著称。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器