简体   繁体   English

如何检查一个数字的每个数字是否大于或等于另一个数字?

[英]How to check every digit of a number is greater or equal than another number?

Every digit should be greater or equal than another. 每个数字都应大于或等于另一个数字。 If all digit are equal, return false. 如果所有数字均相等,则返回false。

Examples: 例子:

201 >= 200 true
200 >= 200 false
200 >= 101 false
210 >= 201 false

The ordinary way is constantly divide by 10, and then compare the remainder. 普通的方法是不断除以10,然后比较余数。

Here is the code in Java: 这是Java中的代码:

private boolean isScoreBetter(final int score, final int scoreToCompare) {
    int a = score;
    int b = scoreToCompare;
    int betterCount = 0;
    while (a > 0 && b > 0) {
        int temp = a % 10 - b % 10;
        if (temp < 0) {
            return false;
        }
        if (temp > 0) {
            betterCount++;
        }
        a /= 10;
        b /= 10;
    }
    return betterCount > 0 && a >= b;
}

Is there a better way? 有没有更好的办法? Definition of "better": “更好”的定义:

  1. Code need to be short and elegant 代码必须简洁明了
  2. The algorithm is better based on digital calculations, and do not contains type conversion like int -> string etc. 该算法基于数字计算更好,并且不包含类型转换,例如int-> string等。

Constraint of the two numbers: 两个数字的约束:

  1. They are non-negative number 它们是非负数
  2. The number of digits is not necessarily the same 位数不一定相同

Thank you for your reply. 谢谢您的回复。

str1 = '000'
str2 = '111'

all(a >= b for a, b in zip(str1, str2))
# False

all(a >= b for a, b in zip(str2, str1))
# True

If you don't want to convert the number to a string (as you said in your edit), there's still more than one way to do it. 如果您不想将数字转换为字符串(如您在编辑中所说),则还有多种方法可以实现。

You can write an iterator that give you the next digit: 您可以编写一个迭代器,为您提供下一位数字:

def next_digit(number):
    while (number > 1):
        yield number % 10
        number = number // 10

Then you can map over all values and use all like in the other answer: 然后,你可以map在所有值,并使用all像其他答案:

    a = 201
    b = 200

    res = all(map(lambda d: d[0] >= d[1], zip(next_digit(a), next_digit(b))))
    print(res) # True

Or you could use filter and see if there are any digits that do not satisfy the condition: 或者,您可以使用filter ,查看是否有不满足条件的数字:

    res = filter(lambda d: d[0] < d[1], zip(next_digit(a), next_digit(b)))
    print(res) # []

But, if you have numbers with a really large amount of digits, these approaches might be inefficient, because they go through all the digits anyway. 但是,如果您的数字中包含大量数字,则这些方法可能效率不高,因为它们始终会遍历所有数字。

You can write a rolled out for loop and optimize a bit by breaking out of it at the first digit that doesn't match your criteria. 您可以编写一个分期付款的for循环并通过在与您的条件不匹配的第一个数字处将其中断进行优化。

You can do that with an iterator, too: 您也可以使用迭代器来做到这一点:

def next_two_digits(a, b):
    while (a > 1 and b > 1):
        yield (a % 10, b % 10)
        a = a // 10
        b = b // 10

And you can either use it as the previous one: 您可以将其用作上一个:

res = all(map(lambda d: d[0] >= d[1], next_two_digits(a, b)))
print(res) # True

res = filter(lambda d: d[0] < d[1], next_two_digits(a, b))
print(res) # []

Or you can do the unrolled-loop thing: 或者,您可以执行展开循环操作:

matching = True
for d in next_two_digits(a, b):
    if (d[0] < d[1]):
        matching = False
        break
print(matching)

Keep in mind that there is a constraint here on the fact that the two numbers need to have the same amount of digits. 请记住,这两个数字必须具有相同数量的数字,这是一个约束。

暂无
暂无

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

相关问题 如何检查列表中的数字是否大于并延续到列表中的下一个数字 - How to check if number in a list is greater than and in continuation to next number in list 有没有一种更快的方法来检查列表中的某个项目是否大于,小于或等于某个数字? - Is there a faster way to check if an item in a list if it is greater than, less than, equal to a certain number? 如何检查一个数字是否包含多个相同的数字? (Java) - How to check if a number contains more than 1 of the same digit? (Java) 如何检查多个动态数字等于另一个dyanami数字 - how to check multiple of a dynamic number to equal to another dyanami number 检查一个数字是否等于另一个 - Check if one number is equal to another 检查数字是否大于 1 的正则表达式 - Regular expression to check if number is greater than 1 递归方法,打印的数字大于或等于第一个数字且低于最后一个数字 - Recursive method that prints all of the digits in a number that or greater than the first digit and lower than the last digit oracle-我们如何获得大于或等于的最小数字值 - oracle - How do we get the smallest number value that is greater than or equal (如果,否则)实时数据库中的问题检查编号等于或大于 - (if, else) question check number equal or greater in Realtime database 如何声明大小在10位数左右或更大的数字数组? - How to declare a numeric array of size around a 10 digit number or greater?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM