Skip to content

Instantly share code, notes, and snippets.

@JunTaoLuo
Last active June 22, 2018 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JunTaoLuo/48b011889720fa394cbe6fe2b939e4a7 to your computer and use it in GitHub Desktop.
Save JunTaoLuo/48b011889720fa394cbe6fe2b939e4a7 to your computer and use it in GitHub Desktop.
Trivia
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