Vote 投票 0 0
How to calculate the digits of a positive integer?どのように計算して、正の整数を桁の数字ですか?
Angsuman ChakrabortyのAngsuman Chakraborty
January 25th, 2004 2004年1月25日 I just came across the need to calculate the digits of a number. It sounds trivial as we can see the digits & count them easily.私はただ全体の必要性を計算するには、数桁の数字です。 trivialとして聞こえること&数桁の数字を参照してそれらを簡単にします。
The challenge is how to find them (with least effort) computationally?への挑戦は、それらを検索する方法(最小努力の)計算ですか?
Solution:解決策:
1 + (int) (Math.log(i) / base10) 1 + ( int ) ( math.log ( 1 ) / base10 )
where base10 = Math.log(10)どこbase10 = math.log ( 10 )
Filed under提出されて Java Software Javaソフトウェア | |
| |
RSS 2.0 RSS 2.0を | |
Email this Article電子メールこの記事
You may also like to readを読むようにすることも可能 |





January 25th, 2004 at 6:16 am 2004年1月25日は6:16アム
Hmm.うーん。 Did a search and found this.これは、検索およびが見つかりました。 http://mindprod.com/jgloss/widthindigits.html
If you are strictly dealing with ints, it seems like this method is the most optimized as it doesn’t require any method calls or logorithmic computation. INTの場合には厳しく対処する、のように思えるこのメソッドは、最も最適化を必要としない、または任意のメソッド呼び出しlogorithmic計算します。 It performs at most 3 simple comparisons.ほとんどの3単純な比較を実行します。
/** / **
* Counts number decimal digits in a 32 bit signed number. *カウント数桁の数字を10進数を32ビットに署名します。 0 => 1, 9 => 1, 0 = > 1 、 9 = > 1 、
* 99 => 2, 2,147,483,647 => 10 * 99 = > 2 、 2,147,483,647 = > 10
*
* @param x * @ param x
* number whose digits you wish to count. *番号の桁数をカウントしたいです。 Must lie in range 0 ..でうそをつく必要があります。範囲0 ..
* Integer.MAX_VALUE; * integer.max_value ;
*
* @return number of digits in x, eg Integer.toString(x).length() * @ return次の桁数Xで、例えばinteger.tostring国( x ) 。長さ( )
* @author Marc Chappuis * @著者マルクシャピュイ marnic@ludomedia.ch
*/ * /
public static int widthInDigits(int x) {公共の静的なint widthindigits ( int x ) (
// do an unravelled binary search / /バイナリ検索を行うunravelled
if (xもし( x