-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_all.cs
67 lines (55 loc) · 1.2 KB
/
sum_all.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Linq;
public class IntOrArray
{
public bool IsInt { private set; get; }
public int IValue { private set; get; }
public IntOrArray[] AValue { private set; get; }
public IntOrArray(int i)
{
IsInt = true;
IValue = i;
}
public IntOrArray(IntOrArray[] a)
{
IsInt = false;
AValue = a;
}
}
public class SumAllKlass
{
public static void Main()
{
var l = Construct();
var total = SumAll(l);
Console.WriteLine(total);
}
public static IntOrArray Construct()
{
var innerestTwo = new IntOrArray(2);
var innerestThree = new IntOrArray(3);
var innerestArray = new IntOrArray(new IntOrArray[] { innerestTwo, innerestThree });
var innerOne = new IntOrArray(1);
var innerArray = new IntOrArray(new IntOrArray[] { innerOne, innerestArray });
var outterOne = new IntOrArray(1);
var outterTwo = new IntOrArray(2);
var outterThree = new IntOrArray(3);
return new IntOrArray(new IntOrArray[] { outterOne, outterTwo, outterThree, innerArray });
}
public static int SumAll(IntOrArray l)
{
if (l.IsInt)
{
return l.IValue;
}
else
{
int tempTotal = 0;
foreach(var item in l.AValue)
{
tempTotal += SumAll(item);
}
return tempTotal;
}
}
}