C# 如何共享给定的整数及其';有多少LINQ患者的余数是多少?
我有一个当前循环(它不是真实循环,bc无法共享真实循环,但算法是相同的):C# 如何共享给定的整数及其';有多少LINQ患者的余数是多少?,c#,linq,C#,Linq,我有一个当前循环(它不是真实循环,bc无法共享真实循环,但算法是相同的): 班级人员{ 公共字符串名称{get;set;} 公共整数数量{get;set;} 人(){ this.name=''; 这个数量=0; } 人员(字符串名称){ this.name=名称; 这个数量=0; } } 类项目共享{ 剩余项=真; //共享项目计数器 q=0; 公共无效份额(国际总项目数量,IEnumerable people){ //如果仍有项目需要共享,我们将开始跨人共享 //直到没有剩下任何物品 当你还活
班级人员{
公共字符串名称{get;set;}
公共整数数量{get;set;}
人(){
this.name='';
这个数量=0;
}
人员(字符串名称){
this.name=名称;
这个数量=0;
}
}
类项目共享{
剩余项=真;
//共享项目计数器
q=0;
公共无效份额(国际总项目数量,IEnumerable people){
//如果仍有项目需要共享,我们将开始跨人共享
//直到没有剩下任何物品
当你还活着的时候{
每个人都是人{
//将1添加到计数器
q++
//若计数器小于或等于总数量,则我们仍可以共享
//将该项目发送给当前人员
如果(q我不太确定我是否正确理解了您的问题,但正如我从代码中看到的,在while循环的末尾有一个无用的foreach循环
您可以将Console.WriteLine($“name:{p.name}数量:{p.quantity}”);
放在if语句中,避免再次遍历人员列表
下面是您的方法可能的样子:
public void share(int total_item_quantity, IEnumerable<People> people)
{
while (items_remaining)
{
foreach (People p in people)
{
// adds 1 to counter
q++;
if (q <= total_item_quantity)
{
p.quantity++; // Increase quantity by 1
Console.WriteLine($"name:{p.name} quantity: {p.quantity}");
}
else
{
items_remaining = false;
break; // Break loop
}
}
}
}
public void share(整数总计项目数量,IEnumerable人)
{
同时(剩余项目)
{
foreach(人中的人)
{
//将1添加到计数器
q++;
如果(q我不太确定我是否正确理解了您的问题,但正如我从代码中看到的,在while循环的末尾有一个无用的foreach循环
您可以将Console.WriteLine($“name:{p.name}数量:{p.quantity}”);
放在if语句中,避免再次遍历人员列表
下面是您的方法可能的样子:
public void share(int total_item_quantity, IEnumerable<People> people)
{
while (items_remaining)
{
foreach (People p in people)
{
// adds 1 to counter
q++;
if (q <= total_item_quantity)
{
p.quantity++; // Increase quantity by 1
Console.WriteLine($"name:{p.name} quantity: {p.quantity}");
}
else
{
items_remaining = false;
break; // Break loop
}
}
}
}
public void share(整数总计项目数量,IEnumerable人)
{
同时(剩余项目)
{
foreach(人中的人)
{
//将1添加到计数器
q++;
如果(q我写的代码正是你一直在寻找的。非常有趣的问题
using System;
using System.Collections.Generic;
using System.Linq;
namespace Distribution
{
class Program
{
public class People
{
public string Name { get; set; }
public int Quantity { get; set; }
}
public static void Main()
{
var totalQuantity = 5;
var peoples = new List<string> { "John","Jack" };
var count = peoples.Count();
decimal quantityRaw = (decimal)totalQuantity / (decimal)count;
int quantity = (int)Math.Ceiling(quantityRaw);
var finalList = peoples.Select(x =>
new People()
{
Name = x,
Quantity = GetQuantity(quantity, totalQuantity, peoples.IndexOf(x))
});
foreach (var p in finalList)
{
Console.WriteLine($"name:{p.Name} quantity: {p.Quantity}");
}
}
public static int GetQuantity(int qty, int qtyTotal, int idx)
{
return Math.Max(Math.Min(qtyTotal - qty * idx, qty), 0);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间分布
{
班级计划
{
公营阶层人士
{
公共字符串名称{get;set;}
公共整数数量{get;set;}
}
公共静态void Main()
{
var totalQuantity=5;
var peoples=新列表{“John”,“Jack”};
var count=peoples.count();
十进制数量raw=(十进制)总数量/(十进制)计数;
整数数量=(整数)数学上限(quantityRaw);
var finalList=people.Select(x=>
新人()
{
Name=x,
Quantity=GetQuantity(数量、总数量、人员索引)
});
foreach(finalList中的var p)
{
WriteLine($“名称:{p.name}数量:{p.quantity}”);
}
}
公共静态int-GetQuantity(int-qty、int-qtyTotal、int-idx)
{
返回数学最大值(数学最小值(qtyTotal-qty*idx,qty),0);
}
}
}
我写的代码正是您一直在寻找的。非常有趣的问题
using System;
using System.Collections.Generic;
using System.Linq;
namespace Distribution
{
class Program
{
public class People
{
public string Name { get; set; }
public int Quantity { get; set; }
}
public static void Main()
{
var totalQuantity = 5;
var peoples = new List<string> { "John","Jack" };
var count = peoples.Count();
decimal quantityRaw = (decimal)totalQuantity / (decimal)count;
int quantity = (int)Math.Ceiling(quantityRaw);
var finalList = peoples.Select(x =>
new People()
{
Name = x,
Quantity = GetQuantity(quantity, totalQuantity, peoples.IndexOf(x))
});
foreach (var p in finalList)
{
Console.WriteLine($"name:{p.Name} quantity: {p.Quantity}");
}
}
public static int GetQuantity(int qty, int qtyTotal, int idx)
{
return Math.Max(Math.Min(qtyTotal - qty * idx, qty), 0);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间分布
{
班级计划
{
公营阶层人士
{
公共字符串名称{get;set;}
公共整数数量{get;set;}
}
公共静态void Main()
{
var totalQuantity=5;
var peoples=新列表{“John”,“Jack”};
var count=peoples.count();
十进制数量raw=(十进制)总数量/(十进制)计数;
整数数量=(整数)数学上限(quantityRaw);
var finalList=people.Select(x=>
新人()
{
Name=x,
Quantity=GetQuantity(数量、总数量、人员索引)
});
foreach(finalList中的var p)
{
WriteLine($“名称:{p.name}数量:{p.quantity}”);
}
}
公共静态int-GetQuantity(int-qty、int-qtyTotal、int-idx)
{
返回数学最大值(数学最小值(qtyTotal-qty*idx,qty),0);
}
}
}
使用该方法:
获取总项目数量
/人的商。计数
和剩余部分
获取将收到该份额的人数
首先,让我们重温人物
类
namespace-YourApp
{
//...
公营阶层人士
{
公共字符串名称{get;set;}=string.Empty;
公共整数数量{get;set;}=0;
公众人士({}
公众人物(字符串名称)=>this.name=name;
公众人物(字符串名称、整数数量)
{
this.Name=Name;
这个。数量=数量;
}
公共重写字符串ToString()=>$“{Name},{Quantity}”;
}
最好为IEnumerable
类型创建一个扩展方法来分发共享
静态类扩展
{
公共静态无效共享(此IEnumerable people,int amountToShare)
{
var div=Math.DivRem(amountToShare,people.Count(),out int rem);
Math.DivRem(rem,people.Count(),out int-maxPersons);
人物。选择((x,i)=>{
x、 数量=div