我正在開發一個程式,開發人員決定從中創建唯一的 idx和y值。id 是這樣計算的:
id = toInteger(toString(x) toString(x y) toString(y));
知道如何從這些 id 中獲取x和y值嗎?
提前致謝。
uj5u.com熱心網友回復:
使用Random類在給定范圍內生成一個亂數。此示例提示用戶輸入隨機生成數字的下限和上限。然后,使用您的代碼段生成 ID。Long如果您的上限很大,我曾經持有該值。
public class RandomInRange {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the lower limit value: " );
int lowerLimit = input.nextInt();
System.out.print("Enter the upper limit value: " );
int upperLimit = input.nextInt();
input.close();
Random random = new Random();
int x = random.nextInt(lowerLimit, upperLimit 1);
int y = random.nextInt(lowerLimit, upperLimit 1);
Long id = Long.valueOf(String.valueOf(x) String.valueOf(x y) String.valueOf(y));
System.out.println("x = " x "; y = " y);
System.out.println("ID: " id);
}
}
我的一次運行的輸出:
Enter the lower limit value: 43
Enter the upper limit value: 87
x = 43; y = 85
ID: 4312885
如您所見,ID 是 43 - 128 - 85。128 是 85 43。巧合的是,“43”等于下限。
另一個示例運行:
Enter the lower limit value: 43
Enter the upper limit value: 87
x = 53; y = 66
ID: 5311966
由于隨機化函式的上界是獨占的,所以我在用戶輸入的數字上加了1,使上限值包含在內。
uj5u.com熱心網友回復:
x 的位數介于 1 和 之間(number of digits of id) - 2。y 中的位數介于 1 和 之間(number of digits in id) - (number of digits in x) - 1。x y 中的位數是其余的。
此方法回傳 2 元素整數陣列中的 x 和 y 對。如果沒有找到,則回傳 null。
static int[] originalXandY(int id) {
String idStr = Integer.toString(id);
int idLen = idStr.length();
for (int xLen = id < 0 ? 2 : 1; xLen <= idLen - 2; xLen) {
int x = Integer.parseInt(idStr.substring(0, xLen));
for (int yLen = 1; yLen <= idLen - xLen - 1; yLen) {
int y = Integer.parseInt(idStr.substring(idLen - yLen));
if (x y == Integer.parseInt(idStr.substring(xLen, idLen - yLen)))
return new int[] {x, y};
}
}
return null;
}
和
System.out.println(Arrays.toString(originalXandY(121)));
System.out.println(Arrays.toString(originalXandY(124634)));
System.out.println(Arrays.toString(originalXandY(1346345)));
System.out.println(Arrays.toString(originalXandY(123412439)));
System.out.println(Arrays.toString(originalXandY(123456)));
System.out.println(Arrays.toString(originalXandY(22220)));
System.out.println(Arrays.toString(originalXandY(-112)));
輸出:
[1, 1]
[12, 34]
[1, 345]
[1234, 9]
null
[2, 20]
[-1, 2]
注意:
如果 id 是22220有兩個解決方案
x = 22, y = 0和x = 2, y = 20. 請注意,此方法僅回傳一個解決方案。
uj5u.com熱心網友回復:
x這是另一種解決方案,它應該回傳y滿足條件的所有可能值。
- 我為生成和反轉程序創建了 lambdas。
- 生成 lambda 回傳一個字串以避免在轉換為 int 時可能發生的溢位。
Set<XY>如果有多個解決方案,我會使用一系列記錄來保存結果。一個類也可以達到這個目的。x這不支持or的負值y。
這使用兩個嵌套回圈。第一個回圈在 ID 上向前迭代,而嵌套回圈在結尾處開始向后迭代。如果從這兩個值生成的 id 等于源 ID,則這些值將存盤在記錄中并添加到集合中。完成后,回傳集合。
record XY(int x, int y) {}
BiFunction<Integer, Integer, String> getId = (a, b) -> Integer.toString(a)
Integer.toString(a b) b;
Function<String, Set<XY>> reverse = (id) -> {
Set<XY> results = new HashSet<>();
for (int i = 0; i < id.length(); i ) {
long first = Long.parseLong(id.substring(0, i 1));
for (int k = id.length() - 1; k > i; k--) {
long last = Long.parseLong(id.substring(k));
if (getId.apply((int) first, (int) last).equals(id)) {
results.add(new XY((int) first, (int) last));
}
}
}
return results;
};
Random r = new Random();
for (int i = 0; i < 10; i ) {
int x = r.nextInt(100);
int y = r.nextInt(200);
String id = getId.apply(x,y);
System.out.printf("= = -> s : %s\n", x,y, id,
reverse.apply(id));
}
列印類似的東西
83 50 -> 8313350 : [XY[x=83, y=50]]
70 34 -> 7010434 : [XY[x=70, y=34]]
63 175 -> 63238175 : [XY[x=63, y=175]]
60 2 -> 60622 : [XY[x=60, y=2]]
56 29 -> 568529 : [XY[x=56, y=29]]
10 23 -> 103323 : [XY[x=10, y=23]]
94 104 -> 94198104 : [XY[x=94, y=104]]
89 51 -> 8914051 : [XY[x=89, y=51]]
78 150 -> 78228150 : [XY[x=78, y=150]]
40 5 -> 40455 : [XY[x=40, y=5]]
并進一步說明Ole VV在評論中的觀察。
String id = getId.apply(1, 30);
Set<XY> res = reverse.apply(id);
System.out.printf("= = -> s : %s\n", 1, 30, id, res);
印刷
1 30 -> 13130 : [XY[x=13, y=0], XY[x=1, y=30]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521687.html
標籤:爪哇数学数字身份
