Find the answer to your Linux question:
Results 1 to 6 of 6
Hi long time no see, I just returned for my holiday and I already have a Math Problem stuck in my head that I cant solve. So I thought Id ...
  1. #1
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422

    Math Problem C++

    Hi long time no see, I just returned for my holiday and I already have a Math Problem stuck in my head that I cant solve.
    So I thought Id bruteforce it with C++
    But I have to do one thing and I have no idea how..

    I have 2 variables A and B.
    B is A Swapped.
    So, if A equals 1 B also equals 1. But if A equals 20 B equals 2.
    So Basicly you'll get this:
    A = 1 B = 1
    A = 12 B = 21
    A = 23 B = 32
    A = 34 B = 43
    A = 45 B = 54

    I hope I made a Sketch of the Problem now.
    Does anyone know how to solve this??

    Thanks,

    Cheers,
    Robin
    New Users, please read this..
    Google first, then ask..

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I could tell you, but then I'd have to kill you. (Just kidding.)

    Why not have a go at it yourself and post what you have?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    This might help

    #include <iostream>

    int main(int argc, char**argv)
    {
    char ch[2];
    ch[1] = '\0';
    int x = 0;
    int y = 0;
    int count = 1;
    const int MULTI = 1;

    std::cout<<"enter a number->";
    while ((ch[0] = std::cin.get()) != '\n')
    {
    if (isdigit(ch[0]))
    {
    y = strtol(ch, NULL, 10);
    x = (MULTI * count) * y + x;
    count = count * 10;
    }
    }
    std::cout<<"x->"<<x<<"\n";
    return 0;
    }
    This is not exactly what your looking for...but its a start

  4. #4
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    Quote Originally Posted by wje_lf View Post
    I could tell you, but then I'd have to kill you. (Just kidding.)

    Why not have a go at it yourself and post what you have?
    I did give it a go myself but my code was really privative.
    What I basicly did wsa:
    Code:
    while (A >= 10)
    {
       if (A >= 10)
       {
          B++;
       }
    }
    B += A * 10;
    But then I would get into problems with 123 and 1453943 Right?

    Anyhow, thanks a lot already.

    Cheers,
    Robin
    New Users, please read this..
    Google first, then ask..

  5. #5
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Use Modulus (&#37 operator to extract last digit.
    e.g.
    A =123
    B = ( A % 10 )
    B will get 3. Its remainder of A / 10.
    You can extract every digit and do whatever you want.

    Armstrong, Palindrome or Reverse numbers.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Two possibilies, the second function should be faster:

    Function with modulo operator:

    Code:
    #include <stdio.h>
    
    int reverse(int value);
    
    int main ()
    {
        int val;
    
        scanf("&#37;d", &val);
        printf("Reverse value: %d\n", reverse(val));
        return(0);
    }
    
    int reverse(int value)
    {
        int val, rev = 0;
        
        val=value;
        
        while(val) {
    	rev*=10;
    	rev+=val%10;
    	val/=10;
        }
        return rev;
    }
    String version:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int reverse(int value);
    
    int main ()
    {
        int val;
    
        scanf("%d", &val);
        printf("Reverse value: %d\n", reverse(val));
        return(0);
    }
    
    int reverse(int value)
    {
        char str[20], rts[20];
        int i = 0, len; 
    
        sprintf(str, "%d", value); 
        len = strlen(str) - 1;
    
        while(str[i]) {
    	rts[i]=str[len];
    	i++;
    	len--;
        }
        rts[i] = '\0';
        return atoi(rts);
    }

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...