简体   繁体   中英

What is the best way to get right most number in an integer with C?

I'm just mucking around with C as a learner, and wrote this little function ...

char *getPlaceSuffix(int number) {

    static char *suffixes[] = {"st", "nd", "rd", "th"};

    if (number >= 11 && number <= 13) {
        return suffixes[3];
    } else {
        while (number > 10) {
            number -= 10;   
        }

        if (number >= 1 && number <= 3) {
            return suffixes[number - 1];
        } else {
            return suffixes[3];
        }               
    }
}   

I tweeted the link, and Konrad Rudolph informed me my method of getting the least significant number was O(n) and not very efficient.

Unfortunately, it's O(n) for very large number – to make it O(logn), adjust the while loop for higher powers of 10 …

Source

I'm not too familiar with Big O notation, but I get the gist that O(n) isn't too efficient?

As you can see from my code sample, I deduct 10 until the number is one digit long, so I can compare it to see which suffix is appropriate. I had a quick play with division and modulus but couldn't figure it out.

So, my question is, what is the best way to get the least significant digit in a number?

I'm still learning, so please go easy on me :)

Thanks!

number % 10

应该管用。

"had a quick play with division and modulus but couldn't figure it out."

 number = number % 10

will do it

And if we're really bothered about efficiency for this code (why?) then

char *getPlaceSuffix(int number) {

    static char *suffixes[] = {"th", "st", "nd", "rd", "th",  "th", "th", "th", "th", "th"};
    int h = number %100;
    int d = number %10
    return (h == 11 or h == 12 or h == 13)? suffixes[0]:suffixes[d];
} 

If number is positive, then number = number % 10; has the same effect as while (number >= 10) number -= 10; . Note that I've used >= , not > as in your code.

So, cover the case of getting 0 as the result of the modulus, and you should be good.

[Edit: oops, your code already would handle that 0 , correctly selecting th . So you're fine. What modulus code did you try?]

"I get the gist that O(n) isn't too efficient?"

Your code isn't too efficient. O(n) means that in the limit as n approaches infinity, your code takes time no worse than proportional to n . In fact we don't really need to worry about limits at infinity, and we don't need to worry about bounds - your loop executes a number of times approximately proportional to n .

In this case, that's inefficient because a much faster solution exists. By definition, a solution which is O(1) is also O(n), so O(n) doesn't actually mean "inefficient", because it specifies an upper bound on time, not a lower bound. Beware that the notation is widely abused, though. People often say "O(n)" to mean that some algorithm takes time at least proportional to n, when they should say "Ω(n)" or "Θ(n)".

模量:

number = number % 10;

The function that you have posted on your web site is incorrect. What will it return if we pass it 113?

char *getPlaceSuffix(int number) {
  static char *suffixes[] = {"th", "st", "nd", "rd"};

  if (number >= 11 && number <= 13) { return suffixes[0]; }
  else {  
    number %= 10;

    if (number >= 1 && number <= 3) { return suffixes[number]; }
    else { return suffixes[0]; }
  }
}

113 will be mod'd by 10, which will result in 3, then it will return suffixes[3], which is "rd".

113 should be 113th, not 113rd (which I'm assuming is pronounced "one-hundred thirturd" -- funny, but not correct.)

Here is how the function should read:

char *getPlaceSuffix(int number) {
  static char *suffixes[] = {"th", "st", "nd", "rd"};

  if (number>100) number %= 100;

  if (number >= 11 && number <= 13) { return suffixes[0]; }
  else {  
    number %= 10;

    if (number >= 1 && number <= 3) { return suffixes[number]; }
    else { return suffixes[0]; }
  }
}

The addition of if (number>100) number %= 100; should make this function return the correct answer.

Can you just take the number and mod it by 10 and that's your least significant number? When you are deducting by 10 until you can't anymore, you're exactly what modulus was meant for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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