Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala找到其数字的五个置换正好是立方体的最小立方体_Scala_Permutation_Cube - Fatal编程技术网

Scala找到其数字的五个置换正好是立方体的最小立方体

Scala找到其数字的五个置换正好是立方体的最小立方体,scala,permutation,cube,Scala,Permutation,Cube,我遇到了这个可怕的问题,并试图在scala中解决它,以便学习该语言。这个问题的简短解释在标题中,这里有一个较长的解释 我用Javascript解决了这个问题,并尝试将逻辑转移到scala,但效果并不理想。它适用于查找3个排列,但不适用于4、5或更高的排列 代码如下: import collection.mutable.Map import collection.mutable.{Map => MMap} val mutMap3 = MMap.empty[String, MMap[Stri

我遇到了这个可怕的问题,并试图在scala中解决它,以便学习该语言。这个问题的简短解释在标题中,这里有一个较长的解释

我用Javascript解决了这个问题,并尝试将逻辑转移到scala,但效果并不理想。它适用于查找3个排列,但不适用于4、5或更高的排列

代码如下:

import collection.mutable.Map
import collection.mutable.{Map => MMap}

val mutMap3 = MMap.empty[String, MMap[String, Int]]
def pow(n:Int) : Int =  { 
  val cubed = (n * n * n).toString
  val digits = 0 to 9
  var str = ""
  for (a <- digits) {
    val b = cubed.count(_.toString==a.toString)
    str += b
  }
  if (mutMap3 contains str) {
    mutMap3(str)("len") += 1
    if(mutMap3(str)("len") == 5) {
      return mutMap3(str)("first")
    }
  } else {
    mutMap3(str) = MMap("len" -> 1, "first" -> n)
  }
  return pow(n+1)
}
import collection.mutable.Map
导入集合.mutable.{Map=>MMap}
val mutMap3=MMap.empty[String,MMap[String,Int]]
DEFPOW(n:Int):Int={
val cubed=(n*n*n).toString
val数字=0到9
var str=“”
对于(a 1,“第一”->n)
}
返回功率(n+1)
}
我的想法是,由于排列只是顺序的重新排列(即4233是3234的排列),那么您可以只计算每个数字出现的次数,这将代表所有可能的排列。在4233的排列中,“0”出现0次,“1”出现0次,“2”出现2次,“4”出现1次,“5”出现0次…等等,一直到9,这可以放在字符串中作为“0012100000”作为整数“0123456789”的出现次数(4233和3234,以及“2”、“3”和“4”的所有其他组合可以表示为“0012100000”)。所以用简单的英语,取一个数字,将其进行立方体运算,将其转换为一个特定整数的字符串,存储该数字,并计算我们在使用数字+1调用函数时看到它的次数,直到特定排列出现5次,然后返回我们第一次得到该排列时的数字


我想看看其他的方法,但我真的很想知道为什么我的方法不起作用,以及它是否是一种好方法。玩得开心!

不是特别有效,但简洁:

scala> val cubes=(0 to 5030).map(c=>c.toLong*c*c).toSet

cubes: scala.collection.immutable.Set[Long] = Set(2628072, ...

scala> cubes.par.filter(_.toString.permutations.map(_.toLong).filter(cubes.contains(_)).length==5)

res6: scala.collection.parallel.immutable.ParSet[Long] = ParSet(38614472000, 10648000000, 10403062487, 91125000000, 65939264000, 54439939000, 95443993000, 122763473000, 116500279104, 114791256000, 40920960536, 103823000000)

scala集合中有方法
permutations
323.toString.permutations.map{{uu.toLong}.toSet-323
=>Set(332233)我必须导入一些东西才能得到那个方法吗?我只是在SimulyScalaWeb上尝试一些东西,它说这个方法不存在。为什么我得到的值不是Scala的集合。您也可以在没有
par
的情况下运行它。浏览scaladoc时,它看起来像
par
permutations
首次出现在2.9.0中