62,046
社区成员
发帖
与我相关
我的任务
分享
void Main()
{
var items = new[] {
new ItemPrice("苹果", 23),
new ItemPrice("梨", 35),
new ItemPrice("香蕉", 47),
new ItemPrice("哈密瓜", 1000),
new ItemPrice("荔枝", 339),
new ItemPrice("柚子", 550),
};
Array.Sort(items, (a,b)=>b.Price.CompareTo(a.Price));
int n = Int32.Parse(Console.ReadLine());
GetCount(n, items).Select(r=>string.Join(", ",r.Zip(items,(a,b)=>new{a,b})
.Select(item=>(item.b.Title+"x"+item.a)))) .Dump();
}
IEnumerable<int[]> GetCount(int n, ItemPrice[] items) {
return GetCount(n, items, new int[items.Length], 0);
}
IEnumerable<int[]> GetCount(int n, ItemPrice[] items, int[] count, int i) {
if (i < items.Length-1 ) {
for(int c=0; n>=0 ;c++, n-=items[i].Price) {
count[i] = c;
foreach(var result in GetCount(n, items, count, i+1))
yield return result;
}
} else {
count[i] = n / items[i].Price;
yield return count;
}
}
struct ItemPrice {
public ItemPrice(string title, int price) {
this.Title = title; this.Price = price;
}
public string Title;
public int Price;
}
linqpd 执行就成
void Main()
{
double price=double.Parse(Console.ReadLine());
var arr=new []{23,35,47,60,1000,339,550};
arr.OrderBy(x=>x).Dump();
int sum=0;
int flag=0;
foreach(int item in arr)
{
sum+=item;
if(sum<=price)
flag++;
else
break;
}
flag.Dump();
}