using System;publicclassStockList{publicstaticstringstockSummary(String[] lstOfArt, String[] lstOf1stLetter){if(lstOfArt.Length ==0){return"";}string result ="";foreach(string m in lstOf1stLetter){int tot =0;foreach(string l in lstOfArt){if(l[0]== m[0]){
tot +=int.Parse(l.Split(' ')[1]);}}if(!String.IsNullOrEmpty(result)){
result +=" - ";}
result +="("+ m +" : "+ tot +")";}return result;}}
答案3:
using System;using System.Collections.Generic;using System.Linq;publicclassStockList{publicstaticstringstockSummary(String[] lstOfArt, String[] lstOf1stLetter){if(lstOfArt.Length ==0|| lstOf1stLetter.Length ==0)returnstring.Empty;
List<string> values =newList<string>();foreach(string s in lstOf1stLetter){int sum = lstOfArt.Where(a => a.StartsWith(s)).Select(a =>int.Parse(a.Split(' ')[1])).Sum();
values.Add(string.Format("({0} : {1})", s, sum));}returnstring.Join(" - ", values.ToArray());}}
using System;using System.Linq;publicclassStockList{publicstaticstringstockSummary(String[] lstOfArt, String[] lstOf1stLetter){if(lstOfArt .Length <=0|| lstOf1stLetter.Length <=0)return"";string result ="";for(var i =0; i <lstOf1stLetter.Length; i++){int number =0;foreach(var art in lstOfArt){if(art.Contains(' ')&& art[0].ToString()== lstOf1stLetter[i]){int artValue;bool numberValid =int.TryParse(art.Split(' ')[1],out artValue);if(numberValid){
number += artValue;}}}
result +="("+ lstOf1stLetter[i]+" : "+ number +")";if(i < lstOf1stLetter.Length -1)
result +=" - ";}return result;}}