如何生成两位数的不同数字PIN码?

huangapple go评论58阅读模式
英文:

How to generate 2-digits distant PIN codes?

问题

我想生成至少在两个数字间隔的4位PIN码。

例如:

  • 5235和4068是可以的
  • 5235和5339是可以的
  • 5235和2553是可以的
  • 5235和5236不可以
  • 5325和5235不可以(排列)

排列很容易检查,但如何检查间隔距离呢?

英文:

I want to generate 4-digits PIN codes that are at least 2-digits distant from each other.

For example:

  • 5235 and 4068 is OK
  • 5235 and 5339 is OK
  • 5235 and 2553 is OK
  • 5235 and 5236 is NOT OK
  • 5325 and 5235 is NOT OK (permutation)

Permutation is easy to check but how to check the distance?

答案1

得分: 1

如果您对于给定的问题描述没有置换方面的问题,似乎您只想检查每个位置的数字是否至少相差2。

在Java中有很多方法可以做到这一点。一种我个人喜欢的“原始”方式是使用基本的数学运算:

public boolean isDistantPins(int pinA, int pinB, int requiredSpacedDigits, int distance) {
    int spacedDigits = 0, aDigit = 0, bDigit = 0;
    while (pinA > 0) {
        aDigit = pinA % 10;    //从右边获取最后一位数字
        bDigit = pinB % 10;    //从右边获取最后一位数字

        if (Math.abs(aDigit - bDigit) >= distance)    //找到差异
            spacedDigits++;

        pinA = pinA / 10;      //从右边移除最后一位数字
        pinB = pinB / 10;      //从右边移除最后一位数字
    }
    return spacedDigits == requiredSpacedDigits;
}

调用方法:

System.out.println(isDistantPins(5235, 4068, 1, 2));
System.out.println(isDistantPins(5235, 5339, 1, 2));
System.out.println(isDistantPins(5235, 2553, 1, 2));
System.out.println(isDistantPins(5235, 5236, 1, 2));
System.out.println(isDistantPins(5235, 5235, 1, 2));

输出:

true
true
true
false
false

请注意,这不会检查排列。因此,您可以在排列检查之后运行此代码(您没有问题),因此此处不显示检查排列的代码。

英文:

If have have no issues with permutation with your given problem description, it seems you only want to check if the digit of each position is having a difference of at least 2.

There are so many ways to do it in Java. One "primitive" way I personally like is using basic math operations:

public boolean isDistantPins (int pinA, int pinB, int requiredSpacedDigits, int distance){
    int spacedDigits = 0, aDigit = 0, bDigit = 0;
    while(pinA > 0 ){
        aDigit = pinA % 10;    //get last digit from the right
        bDigit = pinB % 10;    //get last digit from the right

        if(Math.abs(aDigit - bDigit) >= distance)    //find difference
            spacedDigits ++;

        pinA = pinA / 10;      //remove last digit from the right
        pinB = pinB / 10;      //remove last digit from the right
    }
    return spacedDigits == requiredSpacedDigits;
}

To invoke it:

System.out.println(isDistantPins(5235, 4068, 1, 2));
System.out.println(isDistantPins(5235, 5339, 1, 2));
System.out.println(isDistantPins(5235, 2553, 1, 2));
System.out.println(isDistantPins(5235, 5236, 1, 2));
System.out.println(isDistantPins(5235, 5235, 1, 2));

Output:

true
true
true
false
false

Note that this does not check the permutation. Hence you can run this after your permutation check (which you have no issues) so the codes for checking permutation is not shown here.

答案2

得分: 1

以下是翻译好的内容:

class Solution {
    public static void main(String[] args) {
        /*
         * 5235 和 4068 是合格的
         * 5235 和 5339 是合格的
         * 5235 和 2553 是合格的
         * 5235 和 5236 是不合格的
         * 5325 和 5235 是不合格的(排列)
         */

        // 阈值是在 pin1 和 pin2 足够远时可以相同的最大数字数;在此情况下为 2
        int 阈值 = 2;
        int 密码长度 = 4;

        System.out.println(是否远离(5235, 4068, 阈值, 密码长度));
        System.out.println(是否远离(5235, 5339, 阈值, 密码长度));
        System.out.println(是否远离(5235, 2553, 阈值, 密码长度));
        System.out.println(是否远离(5235, 5236, 阈值, 密码长度));
        System.out.println(是否远离(5325, 5235, 阈值, 密码长度));
    }

    public static boolean 是否远离(int pin1, int pin2, int 阈值, int 密码长度) {
        if (是否排列(pin1, pin2))
            return false;

        int 相同数字数 = 0;
        int 第i位数字1, 第i位数字2;
        for (int i=0; i<密码长度; i++) {
            第i位数字1 = (int) (pin1 / Math.pow(10, i)) % 10;
            第i位数字2 = (int) (pin2 / Math.pow(10, i)) % 10;
            // System.out.println(第i位数字1);
            // System.out.println(第i位数字2);
            if ( 第i位数字1 == 第i位数字2)
                相同数字数 += 1;
        }
        return 相同数字数 <= 阈值;
    }

    private static  boolean 是否排列(int pin1, int pin2) {
        return false;
        // 在这里填写代码
    }
}

请注意,翻译中保留了原始代码中的变量名和注释内容。如果您对翻译有任何疑问或需要进一步的帮助,请随时提问。

英文:

The logic is the following:

  • Check if pin1 is permutation
  • Check every digit of pin1 against the digit in the same position of pin2 using /, %.
  • Count the amount of same digits.
  • Return true/false based on the number of them.

Assuming you have a way to check for permutations, here's a complete solution:

class Solution {
    public static void main(String[] args) {
        /*
         * 5235 and 4068 is OK
         * 5235 and 5339 is OK
         * 5235 and 2553 is OK
         * 5235 and 5236 is NOT OK
         * 5325 and 5235 is NOT OK (permutation)
         */

        // threshold is the maximum number of digits that can be the same
        // while the pin1, pin2 are distant enough; in this case 2
        int threshold = 2;
        int pinLength = 4;

        System.out.println(areDistant(5235, 4068, threshold, pinLength));
        System.out.println(areDistant(5235, 5339, threshold, pinLength));
        System.out.println(areDistant(5235, 2553, threshold, pinLength));
        System.out.println(areDistant(5235, 5236, threshold, pinLength));
        System.out.println(areDistant(5325, 5235, threshold, pinLength));
    }

    public static boolean areDistant(int pin1, int pin2, int threshold, int pinLength) {
        if (isPermutation(pin1, pin2))
            return false;

        int sameDigits = 0;
        int ithDigit1, ithDigit2;
        for (int i=0; i&lt;pinLength; i++) {
            ithDigit1 = (int) (pin1 / Math.pow(10, i)) % 10;
            ithDigit2 = (int) (pin2 / Math.pow(10, i)) % 10;
            // System.out.println(ithDigit1);
            // System.out.println(ithDigit2);
            if ( ithDigit1 == ithDigit2)
                sameDigits += 1;
        }
        return sameDigits &lt;= threshold;
    }

    private static  boolean isPermutation(int pin1, int pin2) {
        return false;
        // fill the code here
    }
}

答案3

得分: 0

计算两个数字的异或。
计算设定位的数量。

英文:

Calculate the XOR of two numbers.
Count the number of set bits.

huangapple
  • 本文由 发表于 2020年9月25日 15:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64059886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定