您的位置:首页 > 编程语言

scala实现相邻两个元素挑换位置的代码,哈哈

2018-01-30 15:01 302 查看
import scala.math._
import breeze.plot._
import breeze.linalg._

import scala.collection.mutable.ArrayBuffer
//https://stackoverflow.com/questions/36984780/spark-shell-how-to-use-breeze-viz
object breeze2 {
def main(args: Array[String]): Unit = {
}

def pow(x: Double, n: Int): Double = {
var pw = 1.0.toDouble
var i = 1
var m = n

for (j <- 1 to (m / 2) if n % 2 == 0 && x > 0) {
pw *= x
}
for (j <- 1 to m if n % 2 == 1 && x > 0) {
pw *= x
}
for (j <- 1 to m if x < 0) {
pw *= 1 / x
}
pw
}

// println(pow(3, 4))

val b = ArrayBuffer(1, 7, 2, 9)
val bSorted = b.sorted

val a = ArrayBuffer(2,3,4,5,6,7)
val i=0
for(i<-0 until  a.length)
{ for(j<-1 until a.length if (i==j-1) if(i%2==0))
{ val temp=a(i)
a(i)=a(j)
a(j)=temp
}

}

for(t<- a)
{
println(a)
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐