我是C++開發新手,希望有人能夠幫助我處理我一直在嘗試做的事情。用整數的十進制表示計算不同位數的個數
舉個例子,我想要一個函數,給定一個整數輸入,返回它包含的不同位數。
因此,舉例來說,如果我有三個整數:
int a = 19876;
int b = 25644;
int c = 4444;
如果我通過「A」到功能,我希望要返回的數字5。 如果將'b'傳遞給函數,我期望返回'4', 如果將'c'傳入函數,則返回1,因爲它們是不同數字的數目。
有人請說明我該如何實現這一目標嗎?
您需要拆分出個別數字,以便對它們進行計數。 'a%10'是'a'的最右邊的數字; 「a/10」是「a」的其餘部分,最右邊的數字被刪除。 –
有沒有你可以提供給我的任何鏈接,以便閱讀這種方法?我發現很難碰到,我通常使用搜索引擎相當好:)但我不能找到這麼容易 –
我不知道任何東西,但它是一個基本的技術,當你需要將數值轉化爲其他。比如說,文本:'while(a!= 0){std :: cout <<(a%10 +'0'); a/= 10; }' –