-
-
Save JunTaoLuo/48b011889720fa394cbe6fe2b939e4a7 to your computer and use it in GitHub Desktop.
Trivia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int ArrLen = 6; | |
const int MaxNum = 6; | |
public static void Main(string[] args) | |
{ | |
// initialize | |
var rnd = new Random(); | |
var nums = new long[ArrLen]; | |
for (var i = 0; i < ArrLen; i++) | |
{ | |
var num = rnd.Next(1, MaxNum); | |
Console.Write(num + ", "); | |
nums[i] = num; | |
} | |
Console.WriteLine(); | |
// compute products to the left and products to the right | |
var lProds = new long[ArrLen]; | |
var rProds = new long[ArrLen]; | |
var lProd = 1l; | |
var rProd = 1l; | |
for (var i = 0; i < ArrLen; i++) | |
{ | |
lProds[i] = lProd; | |
rProds[ArrLen-1-i] = rProd; | |
lProd *= nums[i]; | |
rProd *= nums[ArrLen-1-i]; | |
} | |
// product = left * right | |
for (var i = 0; i < ArrLen; i++) | |
{ | |
Console.Write(lProds[i]*rProds[i] + ", "); | |
} | |
Console.WriteLine(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment