使用给定的数字找到偶数
Find the even number using given number
我必须使用给定数字的数字找到可能的最大偶数
输入:7876541
期望的输出: 8776514
谁能帮我解释逻辑?
这个怎么样?
- 将其转换为字符串
- 将数字倒序排列
- 加入他们并将其转换为数字
def n = 7876541
def newN = (n.toString().split('').findAll{it}.sort().reverse().join()) as Integer
println newN
您可以快速在线试用demo
编辑:根据 OP 评论,更新答案。
这是您可以做的 -
- 找到数字的排列
- 求偶数
- 按最大数量过滤。
已经找到一个 for finding the permutations, so re-using it with little changes. Credits to JavaHopper。
当然可以通过groovified来简化
class Permutations {
static def list = []
public static void printPermutation(char[] a, int startIndex, int endIndex) {
if (startIndex == endIndex)
list << ((new String(a)) as Integer)
else {
for (int x = startIndex; x < endIndex; x++) {
swap(a, startIndex, x)
printPermutation(a, startIndex + 1, endIndex)
swap(a, startIndex, x)
}
}
}
private static void swap(char[] a, int i, int x) {
char t = a[i]
a[i] = a[x]
a[x] = t
}
}
def n = 7876541
def cArray = n.toString().toCharArray()
Permutations.printPermutation(cArray, 0, cArray.size())
println Permutations.list.findAll { it.mod(2) == 0}?.max()
赶紧在线试用demo
无需创建排列。
试试这个解决方案:
- 将源编号转换为字符串。
- 将字符串拆分成数组,
- 对数字进行排序,暂且按升序排列,
- 找到第一个偶数位的索引,
- 从数组中删除这个数字(将其存储在变量中),
- 反转数组并添加删除的数字,
- 加入数组中的数字并将它们转换为整数。
所以整个脚本如下所示:
def inp = 7876541
def chars1 = inp.toString().split('')
// findAll{it} drops an empty starting element from the split result
def chars2 = chars1.findAll{it}.sort()
// Find index of the 1st even digit
def n = chars2.findIndexOf{it.toInteger() % 2 == 0}
def dig = chars2[n] // Store this digit
chars2.remove(n) // Remove from the array
def chars3 = chars2.reverse() // Descending order
chars3.add(dig) // Add the temporarily deleted number
def out = (chars3.join()) as Integer // result
println out
我必须使用给定数字的数字找到可能的最大偶数
输入:7876541
期望的输出: 8776514
谁能帮我解释逻辑?
这个怎么样?
- 将其转换为字符串
- 将数字倒序排列
- 加入他们并将其转换为数字
def n = 7876541
def newN = (n.toString().split('').findAll{it}.sort().reverse().join()) as Integer
println newN
您可以快速在线试用demo
编辑:根据 OP 评论,更新答案。
这是您可以做的 -
- 找到数字的排列
- 求偶数
- 按最大数量过滤。
已经找到一个
当然可以通过groovified来简化
class Permutations {
static def list = []
public static void printPermutation(char[] a, int startIndex, int endIndex) {
if (startIndex == endIndex)
list << ((new String(a)) as Integer)
else {
for (int x = startIndex; x < endIndex; x++) {
swap(a, startIndex, x)
printPermutation(a, startIndex + 1, endIndex)
swap(a, startIndex, x)
}
}
}
private static void swap(char[] a, int i, int x) {
char t = a[i]
a[i] = a[x]
a[x] = t
}
}
def n = 7876541
def cArray = n.toString().toCharArray()
Permutations.printPermutation(cArray, 0, cArray.size())
println Permutations.list.findAll { it.mod(2) == 0}?.max()
赶紧在线试用demo
无需创建排列。 试试这个解决方案:
- 将源编号转换为字符串。
- 将字符串拆分成数组,
- 对数字进行排序,暂且按升序排列,
- 找到第一个偶数位的索引,
- 从数组中删除这个数字(将其存储在变量中),
- 反转数组并添加删除的数字,
- 加入数组中的数字并将它们转换为整数。
所以整个脚本如下所示:
def inp = 7876541
def chars1 = inp.toString().split('')
// findAll{it} drops an empty starting element from the split result
def chars2 = chars1.findAll{it}.sort()
// Find index of the 1st even digit
def n = chars2.findIndexOf{it.toInteger() % 2 == 0}
def dig = chars2[n] // Store this digit
chars2.remove(n) // Remove from the array
def chars3 = chars2.reverse() // Descending order
chars3.add(dig) // Add the temporarily deleted number
def out = (chars3.join()) as Integer // result
println out