本文整理汇总了C#中Queue.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# Queue.Sum方法的具体用法?C# Queue.Sum怎么用?C# Queue.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Queue
的用法示例。
在下文中一共展示了Queue.Sum方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Calculate
public virtual IObservable<double> Calculate(IObservable<double> samples)
{
var q = new Queue<double>();
var sma = double.NaN;
return samples.Select(x =>
{
q.Enqueue(x);
if (double.IsNaN(sma))
{
sma = x;
return x;
}
if (q.Count <= N)
{
sma = q.Sum() / q.Count;
}
else
{
sma = sma + (x / N) - (q.Dequeue() / N);
}
return sma;
});
}
示例2: YieldSequence
public IEnumerable<int> YieldSequence()
{
var queue = new Queue<int>();
queue.Enqueue(1);
yield return 1;
while (true)
{
var next = queue.Sum();
queue.Enqueue(next);
if (queue.Count > 2)
{
queue.Dequeue();
}
yield return next;
}
}
示例3: Solve
public static long Solve()
{
var longest = 0;
var count = 0;
var limit = 1000000;
var primes = new PrimeNumbers(limit);
var test = 0;
var queue = new Queue<int>();
foreach (var prime in primes)
{
test += prime;
if (test >= limit)
break;
queue.Enqueue(prime);
if (primes.IsPrime(test) && queue.Count > count)
{
longest = test;
count = queue.Count;
}
}
while (!primes.IsPrime(test))
{
queue.Dequeue();
test = queue.Sum();
if (primes.IsPrime(test) && queue.Count > count)
{
longest = test;
count = queue.Count;
}
}
return longest;
}
示例4: SendImpl
private void SendImpl(Queue<string> stats)
{
try
{
if (Config.InternalStatsEnabled)
{
PerfLogger.Gauge("Messages", stats.Count);
PerfLogger.Gauge("MessageSize", stats.Sum(s => s.Length));
var stopwatch = Stopwatch.StartNew();
_threadSafeUdpClient.Send(stats);
stopwatch.Stop();
PerfLogger.Timing("Send", stopwatch.ElapsedMilliseconds);
}
else
{
_threadSafeUdpClient.Send(stats);
}
}
catch (Exception e)
{
e.SetContext("stats", stats);
e.SetContext("settings", _settings);
Log.Error("Failed to send stats", e);
if (Config.InternalStatsEnabled)
{
PerfLogger.Increment("Error");
}
}
}
示例5: FindSubtreeByGivenSum
private static void FindSubtreeByGivenSum(int neededSum)
{
foreach (var node in nodeByValue.Values)
{
var queue = new Queue<int>();
InOrderTraverse(node, queue);
if (queue.Sum() == neededSum)
{
subtreeWhitGivenSum.Add(string.Join(" + ", queue));
}
}
}
示例6: AustraliaRuleZ
private int AustraliaRuleZ(TurnManager turnManager, RiskAnalysis analysis, int availableTroops)
{
if (availableTroops == 0) return 0;
var downUnder = analysis.Continents.FirstOrDefault(x => x.Name == EContinent.Australia);
if (downUnder == null || downUnder.MyCountries.Count == 0)
return 0;
if (downUnder.IsOwned)
{
availableTroops = Math.Min(availableTroops, 3);
var whatchamacallit = downUnder.MyCountries.First(x => x.IsContinentGateway());
var minAussieDefenders = 6;
if (whatchamacallit.IsUnderAttack() || whatchamacallit.NumberOfTroops < minAussieDefenders)
{
analysis.Deployments.Add(new Deployment(turnManager)
{
ToCountry = whatchamacallit,
Troops = availableTroops,
});
return availableTroops;
}
var thingamajig = whatchamacallit.AdjacentCountries.First(x => x.Continent != EContinent.Australia);
if (thingamajig.IsUnderAttack() || thingamajig.NumberOfTroops < minAussieDefenders)
{
analysis.Deployments.Add(new Deployment(turnManager)
{
ToCountry = thingamajig,
Troops = availableTroops,
});
return availableTroops;
}
return 0;
}
var deployments = downUnder.MyCountries.Select(x => new Deployment(turnManager) { ToCountry = x });
// round-robin deployment, using a queue...
var q = new Queue<Deployment>(deployments);
while (availableTroops > 0)
{
availableTroops--;
var c = q.Dequeue();
c.Troops++;
q.Enqueue(c);
}
analysis.Deployments.AddRange(q);
return q.Sum(x => x.Troops);
}
示例7: RescueLastManStanding
private int RescueLastManStanding(TurnManager turnManager, RiskAnalysis analysis, int availableTroops)
{
if (availableTroops < 4) return 0;
var lastManStanding = new Queue<Deployment>(from cnt in analysis.Continents
where cnt.MyCountries.Sum(x => x.NumberOfTroops) < 3
from c in cnt.MyCountries
select
new Deployment(turnManager)
{
ToCountry = c
});
if (lastManStanding.Count == 0)
return 0;
while (availableTroops > 0)
{
availableTroops--;
var x = lastManStanding.Dequeue();
x.Troops++;
lastManStanding.Enqueue(x);
}
analysis.Deployments.AddRange(lastManStanding);
return lastManStanding.Sum(x => x.Troops);
}