Advertisement
deflorator1980

Untitled

Nov 30th, 2017
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.99 KB | None | 0 0
  1. object AppS2 {
  2.     def main(args: Array[String]) {
  3.         val quantity = Integer.parseInt(args(0))
  4.         var arrSize = ""
  5.         1 to quantity foreach {_ => arrSize += "1"}
  6.         val numberDec = Integer.parseInt(arrSize, 2)
  7.         println(numberDec)
  8.         var sum0, sum1 = 0
  9.         val arr = Array.fill(quantity){scala.util.Random.nextInt(10)}
  10.         val res = new Array[Int](numberDec)
  11.         arr.foreach({a => print(a + " ")})
  12.  
  13.         for (n_dec <- 1 to numberDec) {
  14.           for (n_bin <- 0 until arr.length) {
  15.                 if (getBit(n_dec, n_bin) == 0) {
  16.                     sum0 += arr(n_bin)
  17.                 } else if (getBit(n_dec, n_bin) == 1) {
  18.                     sum1 += arr(n_bin)
  19.                 }
  20.             }
  21.             res(n_dec-1) = Math.abs(sum1 - sum0)
  22.             sum0 = 0; sum1 = 0
  23.         }
  24.         println("\n" + res.reduce(_ min _))
  25.     }
  26.     def getBit (nDec: Int, nBin: Int) =  {
  27.         val x = (nDec >> nBin) & 1
  28.         x
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement