Number to digits without using strings or dividing by 10

So, there is such a task in which you need to write code that breaks a number between 0-999 into digits without using a string or dividing by 10. I tried so hard and could not come up with a perfect algorithm, I got my code to split the numbers 1-99 but I really think there is some better alternative without using 111 if statements. Ok, so here is what I got:

#include <iostream>

int main() {
    std::cout << "Enter a number ";
    int number;
    std::cin >> number;

    int cycles;
    if (number > 100) {
        cycles = 3;
    }
    else if (number > 10) {
        cycles = 2;
    }
    else {
        cycles = 1;
    }

    int digit[] = { -1, -1, -1 };
    for (int i = 0; i < cycles; i++) {
        if (number < 10) {
            digit[0] = number;
        }
        else if (number < 100) {
            if (number < 20) {
                digit[1] = number - 10;
                number = 1;
            }
            else if (number < 30) {
                digit[1] = number - 20;
                number = 2;
            }
            else if (number < 40) {
                digit[1] = number - 30;
                number = 3;
            }
            else if (number < 50) {
                digit[1] = number - 40;
                number = 4;
            }
            else if (number < 60) {
                digit[1] = number - 50;
                number = 5;
            }
            else if (number < 70) {
                digit[1] = number - 60;
                number = 6;
            }
            else if (number < 80) {
                digit[1] = number - 70;
                number = 7;
            }
            else if (number < 90) {
                digit[1] = number - 80;
                number = 8;
            }
            else {
                digit[1] = number - 90;
                number = 9;
            }
        }
        else if (number < 1000) {
            if (number < 200) {
                number -= 100;
            }
            else if (number < 300) {
                number -= 200;
            }
            else if (number < 400) {
                number -= 300;
            }
            else if (number < 500) {
                number -= 400;
            }
            else if (number < 600) {
                number -= 500;
            }
            else if (number < 700) {
                number -= 600;
            }
            else if (number < 800) {
                number -= 700;
            }
            else if (number < 900) {
                number -= 800;
            }
            else {
                number -= 900;
            }
        }
    }

    for (int i = 0; i < 3; i++) {
        if (digit[i] != -1) {
            std::cout << digit[i] << " ";
        }
    }
    std::cout << "\n";

    std::cout << "Press any key to exit... ";
    char i;
    std::cin >> i;
    return 0;
}

I'm stuck, so if someone can help me, it will be very grateful!

+4
source share
5 answers

, . , .. 10 0-9, , . , - . , , (1,10 100) , ,

int getDigit(int base, int number) {
  int digit = 0;
  for (int i = base;i <= number;i += base) ++digit;
  return digit;
}

, < 10 * . . * , .

+3

, , , , . - :

int number;
int subtractionvalue;
int[3] placevalues;
for(int thisvalue=2;thisvalue>=0;thisvalue--){
    subtractionvalue=round(pow((double)10,thisvalue));
    while(number>=subtractionvalue){
        number=number-subtractionvalue;
        placevalues[thisvalue]++;
    }
}

placevalues ​​ .

+2

10, , 10, . :

10 :

A: Q:

Q = ((A >>  1) + A) >> 1; 
Q = ((Q >>  4) + Q)     ; 
Q = ((Q >>  8) + Q)     ; 
Q = ((Q >> 16) + Q) >> 3; 
/* either Q = A/10 or Q+1 = A/10 for all 32-bit unsigned A */

:

R=A-10*Q;

, while, , 999, [3]:

#include<stdio.h>
#include<stdlib.h> 
int main()
{
    int A=123;
    int R=A;
    int Q=123; /* the quotient */

    Q = ((A >>  1) + A) >> 1;

    int ar[3]={0},i=2;

    //loop for getting all digits
    while(Q>10)
    {
       Q = ((Q >>  4) + Q)     ;
       Q = ((Q >>  8) + Q)     ;
       Q = ((Q >> 16) + Q) >> 3;
       //storing ramainder in array in reverse
       ar[i--]=R-10*Q;
       R=Q;
    }
    ar[i]=Q;
    for(i=0;i<3;i++)
      printf("%d ",ar[i]);
    return 0; 
}
+1

#, ?

static void Main(string[] args)
{
  int number = 592;
  int digit1 = 0;
  int digit2 = 0;
  int digit3 = 0;
  for (int c = 0; c < number; c++)
  {
    digit1 += 1;
    if (digit1 == 10)
    {
      digit2 += 1;
      digit1 = 0;
    }
    if(digit2 == 10)
    {
      digit3 += 1;
      digit2 = 0;
    }
  }
  Console.WriteLine(digit1);
  Console.WriteLine(digit2);
  Console.WriteLine(digit3);
}

++:

int main()
{
  std::cout << "Enter a number ";
  int number;
  std::cin >> number;
  int digit1 = -1;
  int digit2 = -1;
  int digit3 = -1;
  for (int c = 0; c < number; c++)
  {
    digit1 += 1;
    if (digit1 == 10)
    {
      digit2 += 1;
      digit1 = 0;
    }
    if (digit2 == 10)
    {
      digit3 += 1;
      digit2 = 0;
    }
  }
  if (digit3 > -1) {
    std::cout << digit3+1 << " ";
  }
  if (digit2 > -1) {
    std::cout << digit2+1 << " ";
  }
  if (digit1 > -1) {
    std::cout << digit1+1 << " ";
  }
  std::cout << "\n";

  std::cout << "Press any key to exit... ";
  char i;
  std::cin >> i;
  return 0;
}
+1

, , , , . , 32- int 4 28 .

, 1000. 2 28. , , . ​​ ++, ceil() .

, 10, , .

I saved as much of the source code for the program below as possible.

#include <iostream>
#include <cmath>

int main() {
    std::cout << "Enter a number ";
    int number;
    std::cin >> number;
    int digit[] = { -1, -1, -1 };

    unsigned int t, dec_digit;
    bool have_seen_nonzero;

    t = ceil (number / 1e3 * (1 << 28)); // 4.28 fixed-point representation
    for (int pos = 0; pos < 3; pos++) {
        t = t * 10;            
        dec_digit = t >> 28; // extract integer portion of fixed-point number
        t = t & 0x0fffffff; // discard integer portion of fixed-point number
        have_seen_nonzero = dec_digit != 0;
        if (have_seen_nonzero) digit[pos] = dec_digit;
    }

    for (int i = 0; i < 3; i++) {
        if (digit[i] != -1) {
            std::cout << digit[i] << " ";
        }
    }
    std::cout << "\n";

    std::cout << "Press any key to exit... ";
    char i;
    std::cin >> i;
    return 0;
}
+1
source

Source: https://habr.com/ru/post/1612155/


All Articles