Water collected between towers: Difference between revisions
Content added Content deleted
(→{{header|Visual Basic .NET}}: Added second version, which doesn't depend on Replace() statements, and has some formatting details..) |
(→{{header|C sharp|C#}}: Added translation of VB.NET version 2 without verbose output option.) |
||
Line 305: | Line 305: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
===Version 1=== |
|||
Translation from [[{{FULLPAGENAME}}#Visual_Basic_.NET|Visual Basic .NET]]. See that entry for code comments and details. |
Translation from [[{{FULLPAGENAME}}#Visual_Basic_.NET|Visual Basic .NET]]. See that version 1 entry for code comments and details. |
||
<lang Csharp>using System;</lang> |
<lang Csharp>using System;</lang> |
||
<lang Csharp>static void Main(string[] args) |
<lang Csharp>static void Main(string[] args) |
||
Line 340: | Line 341: | ||
Block 6 retains 0 water units. |
Block 6 retains 0 water units. |
||
Block 7 retains 0 water units.</lang> |
Block 7 retains 0 water units.</lang> |
||
===Version 2=== |
|||
Conventional "scanning" algorithm, translated from [[{{FULLPAGENAME}}#Version_2_2|the second version of Visual Basic.NET]], but (intentionally tweaked to be) incapable of verbose output. See that version 2 entry for code comments and details. |
|||
<lang cSharp>static void Main(string[] args) { |
|||
int i = 1; int[][] a = {new int[] { 1, 5, 3, 7, 2 }, |
|||
new int[] { 5, 3, 7, 2, 6, 4, 5, 9, 1, 2 }, |
|||
new int[] { 2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1 }, |
|||
new int[] { 5, 5, 5, 5 }, new int[] { 5, 6, 7, 8 }, |
|||
new int[] { 8, 7, 7, 6 }, new int[] { 6, 7, 10, 7, 6 }}; |
|||
foreach (int[] t in a) { |
|||
string b = "", lf = "\n"; int j; do { |
|||
for (j = t.Length - 1; j >= 0; j--) if (t[j] > 0) break; |
|||
if (j < 0) break; |
|||
char[] f = new string(' ', t.Length).ToCharArray(); |
|||
int bpf = 0; for (int k = 0; k <= j; k++) { |
|||
if (t[k] > 0) { f[k] = 'B'; t[k] -= 1; bpf += 1; } |
|||
else if (bpf > 0) f[k] = '.'; } |
|||
if (bpf > 1) b = new string(f) + (b == "" ? "" : lf) + b; |
|||
else break; |
|||
} while (true); |
|||
int c = 0; string v = "hold"; foreach (char x in b) if (x == '.') c++; |
|||
System.Console.WriteLine(string.Format("Block {0} {1} water units.", i++, |
|||
c == 0 ? "does not " + v + " any" : v + "s " + c.ToString())); } }</lang> |
|||
'''Output:''' |
|||
<lang>Block 1 holds 2 water units. |
|||
Block 2 holds 14 water units. |
|||
Block 3 holds 35 water units. |
|||
Block 4 does not hold any water units. |
|||
Block 5 does not hold any water units. |
|||
Block 6 does not hold any water units. |
|||
Block 7 does not hold any water units.</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |