Skip to content

Instantly share code, notes, and snippets.

@liango2
Created January 14, 2016 21:04
Show Gist options
  • Save liango2/b1f30c88010b345db6c6 to your computer and use it in GitHub Desktop.
Save liango2/b1f30c88010b345db6c6 to your computer and use it in GitHub Desktop.
统计给定Array[Int]数组中正数, 负数, 0所占的比, 依次(正数, 然后负数,然后0)打印结果,使用小数表示即可
object Solution {
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var n = sc.nextInt();
var arr = new Array[Int](n);
for(arr_i <- 0 to n-1) {
arr(arr_i) = sc.nextInt();
}
List(arr.count(_>0)/n.toDouble,arr.count(_<0)/n.toDouble,arr.count(_==0)/n.toDouble).foreach(println)
List((a: Int) => a > 0, (a: Int) => a < 0, (a: Int) => a == 0).map(arr.count(_) / n.toDouble) foreach println
arr.groupBy(math.signum).mapValues(_.length * 1.0 / arr.length)
.toList.sortWith { case ((_, 0), b) => false case (a, b) => a._2 > b._2 }
.foreach { case (_, b) => println(b) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment