简体   繁体   English

不循环计算数字的位数 C++

[英]Count digits of number without loop C++

I have to count the number of digits in a number.我必须计算一个数字的位数。

I divide the number to 10 until I get 0. Each iteration increments the counter.我将数字除以 10,直到得到 0。每次迭代都会增加计数器。

int num;
cin>>num;  
while(num > 0)  
{  
  counter++;
  num = num / 10;   
}

The challenge is not using any loops or recursion, just an if statement.挑战不使用任何循环或递归,只是一个if语句。

Is it possible?是否可以?

counter = log(num) / log(10)计数器 = 日志(数量)/日志(10)

in c++:在 C++ 中:

#include <cmath>
....
counter = num == 0 ? 1 : log10(std::abs(num)) + 1;

what you want is the log function.你想要的是日志功能。

cplusplus - log10 cplusplus - log10

cplusplus - std::abs cplusplus - std::abs

Easy way although somewhat expensive, turn your number to string and take its size like the example below:简单的方法虽然有点贵,但将您的数字转换为字符串并取其大小,如下例所示:

#include <iostream>
#include <string>

int main() {
  int i = 1232323223;
  std::string str = std::to_string(std::abs(i));
  std::cout << "Number of Digits: " << str.size() <<std::endl;
}

LIVE DEMO现场演示

One way is to use sprintf , since it returns the number of characters emitted:一种方法是使用sprintf ,因为它返回发出的字符数:

int digits(int n)
{
    char s[32];
    int len = sprintf(s, "%d", n);
    if (n < 0) len--; // NB: handle negative case
    return len;
}

Sure it's possible, something like (for 32-bit numbers):当然有可能,例如(对于 32 位数字):

int numDigitsU (unsigned int n) {
    if (n <         10) return 1;
    if (n <        100) return 2;
    if (n <       1000) return 3;
    if (n <      10000) return 4;
    if (n <     100000) return 5;
    if (n <    1000000) return 6;
    if (n <   10000000) return 7;
    if (n <  100000000) return 8;
    if (n < 1000000000) return 9;
    /*      4294967295 is 2^32-1 - add more ifs as needed
       and adjust this final return as well. */
    return 10;
}

The signed variant is a little trickier since the sign is reversed first, and you have to watch out for MININT :有符号的变体有点棘手,因为符号首先被反转,你必须注意MININT

int numDigitsS (int n) {
    if (n == MININT) n = MAXINT;  // same number of digits, usually.
    if (n < 0) n = -n;            // reverse sign.
    return numDigitsU (n);        // call the unsigned variant.
}

Just adjust the highest comparison value and return value based on the size of your largest unsigned int .只需根据最大的unsigned int的大小调整最高比较值和返回值。

This should work with all allowed negative codings: two's complement, ones' complement, and sign/magnitude.这应该适用于所有允许的负编码:二进制补码、一个补码和符号/大小。

It's actually quite easy if you consider that the maximum size of int is finite.如果您认为int的最大大小是有限的,这实际上很容易。 Just check if the number is larger than 10, larger than 100, larger than 1000, etc. You could even do binary search.只需检查数字是否大于 10、大于 100、大于 1000 等。您甚至可以进行二分查找。

int num = abs(number);
if (num < 10000)
{
    if (num < 100)
        return num < 10 ? 1:2;
    else
        return num < 1000 ? 3:4;   
}
else
{
    ...
}

You can find the length of an integer just by doing it as follows:您可以通过执行以下操作来找到整数的长度:

int countDigits(int *num){
    int count =0;
    while(*num>0){
        count++;
        *num /=10;
    }
    return count;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM