本文整理汇总了C#中Stack.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Sum方法的具体用法?C# Stack.Sum怎么用?C# Stack.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.Sum方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Solve
static void Solve(Stack<int> stack, int index)
{
if (index == -1)
{
return;
}
if (stack.Sum() == target)
{
Console.WriteLine(string.Join(", ", stack.Reverse()));
return;
}
else
{
if (stack.Sum() + coins[index] <= target)
{
stack.Push(coins[index]);
}
else
{
index--;
}
Solve(stack, index);
}
}
示例2: Main
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).Distinct().ToArray();
bool flag = false;
int possibleSubsets = (int)Math.Pow(2, arr.Length);
for (int mask = 0; mask <= possibleSubsets; mask++)
{
Stack<int> subset = new Stack<int>();
for (int i = 0; i < arr.Length; i++)
{
if ((mask & (1 << i)) != 0)
{
subset.Push(arr[i]);
}
}
if (subset.Sum() == n && subset.Count != 0)
{
Console.WriteLine(String.Join(" + ", subset) + " = " + n);
flag = true;
}
subset.Clear();
}
if (!flag)
{
Console.WriteLine("No matching subsets.");
}
}
示例3: ConvertRomanNumberToArabic
public int ConvertRomanNumberToArabic(string romanFormatNumber)
{
var result = 0;
var stack = new Stack<int>();
var listOfArabicDigits = new List<int>();
foreach (var currentCharOfRomanNumber in romanFormatNumber)
{
var currentArabicDigit = ConvertRomanDigitToArabic(currentCharOfRomanNumber);
listOfArabicDigits.Add(currentArabicDigit);
}
foreach (var currentArabicDigit in listOfArabicDigits)
{
if (stack.Count != 0)
{
if (currentArabicDigit < stack.Peek())
{
result += stack.Sum();
stack.Clear();
stack.Push(currentArabicDigit);
}
else if (currentArabicDigit == stack.Peek())
{
stack.Push(currentArabicDigit);
}
else
{
var subtrahend = stack.Sum();
var minuend = currentArabicDigit;
var currentArabicDigitAfterSubtraction = minuend - subtrahend;
result += currentArabicDigitAfterSubtraction;
stack.Clear();
}
}
else
{
stack.Push(currentArabicDigit);
}
}
var remainingDigits = stack.Sum();
result += remainingDigits;
return result;
}
示例4: Main
static void Main()
{
coins.Reverse();
Stack<int> result = new Stack<int>();
Solve(result, coins.Length - 1);
if(result.Sum() != target)
Console.WriteLine(-1);
}
示例5: Calculate
public static ANXResult Calculate(this string[] tokens, string xname = "X")
{
int i = 0;
Stack<decimal?> s = new Stack<decimal?>(2);
Stack<decimal> i0 = new Stack<decimal>();
Stack<decimal> i1 = new Stack<decimal>();
while (i < tokens.Length)
{
decimal dectoken;
if (decimal.TryParse(tokens[i], out dectoken))
{
s.Push(dectoken);
}
else if (operators.Keys.Contains(tokens[i]))
{
Action<Stack<decimal?>, Stack<decimal>, Stack<decimal>> action = operators[tokens[i]];
action(s, i1, i0);
}
else if (xname.Equals(tokens[i]))
{
s.Push(null);
i1.Push(1);
i0.Push(0);
}
else
{
throw new Exception(string.Format("Unknown type for token {0}", tokens[i]));
}
#if DEBUG
Console.WriteLine(string.Format("{4}\t{0}\t{1}\t{2}\t{3}", s.Content(), tokens[i], i1.Content(), i0.Content(), i));
#endif
i++;
}
if (s.Count != 1)
{
throw new Exception("Invalid expression.");
}
decimal sc = s.Sum((d2) => { if (d2 != null) return d2.Value; else return 0; });
ANXResult result = new ANXResult() { Item1 = i1.Sum(), Item0 = i0.Sum() + sc };
return result;
}
示例6: Main
public static void Main()
{
int rows = int.Parse(Console.ReadLine());
int cols = int.Parse(Console.ReadLine());
var matrix = new Cell[rows, cols];
for (int i = 0; i < rows; i++)
{
int[] currentRow = Console.ReadLine().Split().Select(int.Parse).ToArray();
for (int j = 0; j < cols; j++)
{
matrix[i, j] = new Cell(i, j, currentRow[j]);
}
}
matrix[0, 0].DijkstraDistance = 0;
var queue = new PriorityQueue<Cell>();
queue.Enqueue(matrix[0, 0]);
while (queue.Count > 0)
{
var cell = queue.ExtractMin();
int row = cell.Row;
int col = cell.Col;
if (IsValidCell(row, col + 1, rows, cols))
{
var rightCell = matrix[row, col + 1];
if (rightCell.DijkstraDistance > cell.DijkstraDistance + rightCell.Value)
{
rightCell.DijkstraDistance = cell.DijkstraDistance + rightCell.Value;
rightCell.PreviousCell = cell;
if (queue.Contain(rightCell))
{
queue.DecreaseKey(rightCell);
}
}
if(!rightCell.IsVisited)
{
queue.Enqueue(rightCell);
rightCell.IsVisited = true;
}
}
if (IsValidCell(row + 1, col, rows, cols))
{
var bottomCell = matrix[row + 1, col];
if (bottomCell.DijkstraDistance > cell.DijkstraDistance + bottomCell.Value)
{
bottomCell.DijkstraDistance = cell.DijkstraDistance + bottomCell.Value;
bottomCell.PreviousCell = cell;
if (queue.Contain(bottomCell))
{
queue.DecreaseKey(bottomCell);
}
}
if(!bottomCell.IsVisited)
{
queue.Enqueue(bottomCell);
bottomCell.IsVisited = true;
}
}
if (IsValidCell(row, col - 1, rows, cols))
{
var leftCell = matrix[row, col - 1];
if (leftCell.DijkstraDistance > cell.DijkstraDistance + leftCell.Value)
{
leftCell.DijkstraDistance = cell.DijkstraDistance + leftCell.Value;
leftCell.PreviousCell = cell;
if (queue.Contain(leftCell))
{
queue.DecreaseKey(leftCell);
}
}
if(!leftCell.IsVisited)
{
queue.Enqueue(leftCell);
leftCell.IsVisited = true;
}
}
if (cell.Row == rows - 1 && cell.Col == cols -1)
{
break;
}
}
var lastCell = matrix[rows - 1, cols - 1];
var path = new Stack<int>();
while (lastCell != null)
{
path.Push(lastCell.Value);
lastCell = lastCell.PreviousCell;
}
Console.WriteLine("Length: " + path.Sum());
Console.WriteLine("Path: " + string.Join(" ", path));
}
示例7: GetLevelWithOffsets
public int GetLevelWithOffsets(Stack<EMInclude> includesStack)
{
return includesStack.Sum(include => include.HeaderOffset) + Level;
}
示例8: sumEvenFibonacci
//ach new term in the Fibonacci sequence is generated by adding the previous two terms.
//By starting with 1 and 2, the first 10 terms will be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
public JsonResult sumEvenFibonacci(int maxTerm)
{
Stack<double> fibSeq = new Stack<double>();
Queue<double> fibQueue = new Queue<double>();
//initial 1, 2
Stack<double> fibEvenSeq = new Stack<double>();
Stack<double> fibOddSeq = new Stack<double>();
double evenSumFibSeq = 0;
double newFibNumber = 0;
if(maxTerm ==1)
{
evenSumFibSeq = 0;
}
else if (maxTerm == 2)
{
evenSumFibSeq = 2;
}
else
{
fibOddSeq.Push(1);
fibSeq.Push(1);
fibQueue.Enqueue(1);
fibEvenSeq.Push(2);
fibSeq.Push(2);
fibQueue.Enqueue(2);
while (fibSeq.Peek() < maxTerm)
{
if (fibQueue.Count() >= 2)
{
newFibNumber = fibQueue.Dequeue();
newFibNumber += fibQueue.Peek();
if (newFibNumber % 2 == 0)
{
fibEvenSeq.Push(newFibNumber);
}
else
{
fibOddSeq.Push(newFibNumber);
}
fibSeq.Push(newFibNumber);
fibQueue.Enqueue(newFibNumber);
}
else
{
throw new ArgumentException("Need 2 previous term for new Fibonacci number.");
}
}
evenSumFibSeq = fibEvenSeq.Sum();
}
return Json(evenSumFibSeq, JsonRequestBehavior.AllowGet);
}
示例9: FindPathWhitSum
private static void FindPathWhitSum(Tree<int> root, Stack<Tree<int>> path, int sum)
{
foreach (var child in root.Children)
{
path.Push(child);
FindPathWhitSum(child, path, sum);
if (path.Sum(n => n.Value) == sum)
{
var result = path.ToArray().Reverse();
pathsWhitGivenSum.Add(string.Join(" -> ", result));
}
path.Pop();
}
}
示例10: BacktrackingFullMoveList
/// <summary>
/// Will backtrack the solutions to find the optimize line to pull
/// </summary>
/// <param name="trashIndex">index of the trash line</param>
/// <param name="spaceAvailable">space available in the trash line</param>
/// <param name="stack">stack of the used lines</param>
/// <returns></returns>
private Stack<int> BacktrackingFullMoveList(int trashIndex, int spaceAvailable, Stack<int> stack)
{
var dump = new Stack<int>(stack);
for (int i = 0; i < 6; ++i)
{
if (i != trashIndex && !stack.Contains(i) && _lines[i].SpaceNeeded() <= spaceAvailable)
{
dump.Push(i);
var result = BacktrackingFullMoveList(trashIndex, spaceAvailable - _lines[i].SpaceNeeded(), dump);
if (result.Sum(x => _lines[x].TrashCapacity()) > stack.Sum(x => _lines[x].TrashCapacity()))
stack = new Stack<int>(result);
dump.Pop();
}
}
return stack;
}
示例11: SearchTriangle
static void SearchTriangle(int level, int position, Stack<int> items)
{
if (level < triangle.Length)
{
items.Push(triangle[level][position]);
SearchTriangle(level+1, position, items);
SearchTriangle(level+1, position+1, items);
items.Pop();
}
else
{
if (items.Sum() > _max)
{
_max = items.Sum();
}
}
}
示例12: FindPathsWithGivenSum
// returns also paths that are not strictly starting from the root and finishing to a leaf
private static Stack<int>[] FindPathsWithGivenSum(Tree<int> root)
{
Stack<Tree<int>> dfsStack = new Stack<Tree<int>>();
var foundPaths = new Stack<Stack<int>>();
dfsStack.Push(root);
while (dfsStack.Count > 0)
{
var currentNode = dfsStack.Pop();
var set = new Stack<int>();
var node = currentNode;
while (null != node)
{
set.Push(node.Value);
if (set.Sum() == searchedPathSum)
{
foundPaths.Push(set);
}
node = node.Parent;
}
set = new Stack<int>();
foreach (var child in currentNode.Children)
{
dfsStack.Push(child);
}
}
return foundPaths.ToArray();
}