2011-08-17 15 views

Odpowiedz

22

dalej robić modulo-10 i dzielenia przez 10:

int n; // from somewhere 
while (n) { digit = n % 10; n /= 10; } 

To wypluwa cyfry od najmniej znaczących do najbardziej znaczące. Możesz wyraźnie uogólnić to na dowolną bazę liczbową.

+0

Doskonałe dzięki – daidai

2

Prawdopodobnie chcesz użyć mod i podzielić, aby uzyskać te cyfry.

Coś jak:

Grab first digit: 

    Parse digit: 9802 mod 10 = 2 
    Remove digit: (int)(9802/10) = 980 

Grab second digit: 

    Parse digit: 980 mod 10 = 0 
    Remove digit: (int)(980/10) = 98 

coś takiego.

0

jeśli chcesz wyświetlić cyfry w tej samej kolejności trzeba będzie zrobić moduł dwukrotnie wiza werset jest to kod, który robi:

#import <Foundation/Foundation.h> 
int main (int argc, char * argv[]) 
    { 
    @autoreleasepool { 
    int number1, number2=0 , right_digit , count=0; 
    NSLog (@"Enter your number."); 
    scanf ("%i", &number); 
    do { 
     right_digit = number1 % 10; 
     number1 /= 10; 
    For(int i=0 ;i<count; i++) 
     { 
     right_digit = right_digit*10; 
     } 
    Number2+= right_digit; 
    Count++; 
     } 
    while (number != 0); 
do { 
right_digit = number2 % 10; 
number2 /= 10; 
Nslog(@”digit = %i”, number2); 
} 
while (number != 0); 
} 
} 
return 0; 
} 

mam nadzieję, że jest to przydatne :)

Powiązane problemy