110,552
社区成员
发帖
与我相关
我的任务
分享
List<long> _list = new List<long>();
for(long i<=201102;i<=201208;i++)
{
_list.Add(i);
}
_list.Sort();
如上所示的一个集合,存储的是 年*100+月 格式的数据,如何将这个集合里的数据分组,得到Dictionary<int,List<long>>
格式的数据?
Dictionary<int,List<long>> 中的键为时间集合中的季度顺序,List<long> 为本季度的时间(来自_list集合)
如 key=1的Dictionary<int,List<long>> 的值List<long> 中存储的值为 201102,201103这样。
求算法,在线等!
Dictionary<long, List<long>> seasons = new Dictionary<intlong List<long>>();
for (long i = 201102; i <= 201208; i++)
{
long season = (i % 100 - 1) / 3;
if (!seasons.ContainsKey(season))
seasons.Add(season, new List<long>());
seasons[season].Add(i);
}
List<long> _list = new List<long>();
for(long i<=201102;i<=201208;i++)
{
_list.Add(i);
}
void Main()
{
var _list =Enumerable.Range(201101,12);
var query=_list.GroupBy(l=>GetMonth(l));
int m=1;
var temp=query.ToDictionary(l=>m++);
temp.ToList().ForEach(t=>Console.WriteLine(string.Format("Key: {0}\t Value: {1}",t.Key,string.Join(" ",t.Value.Select(n=>n.ToString()).ToArray()))));
/*
Key: 1 Value: 201101 201102 201103
Key: 2 Value: 201104 201105 201106
Key: 3 Value: 201107 201108 201109
Key: 4 Value: 201110 201111 201112
*/
}
int GetMonth(int time)
{
var temp=time.ToString();
var month=int.Parse(temp.Substring(4,2));
return month%3==0?(month/3)-1:month/3;
}
Dictionary<long, List<long>> seasons = new Dictionary<long, List<long>>();
List<long> _list = new List<long>();
_list.Add(201102);
_list.Add(201103);
_list.Add(201104);
_list.Add(201105);
_list.Add(201106);
_list.Add(201107);
_list.Add(201108);
_list.Add(201109);
_list.Add(201110);
_list.Add(201111);
_list.Add(201112);
_list.Add(201201);
_list.Add(201202);
_list.Add(201203);
_list.Add(201204);
_list.Add(201205);
foreach (long i in _list)
{
long season = (i % 100 - 1) / 3+1;
if (!seasons.ContainsKey(season))
seasons.Add(season, new List<long>());
seasons[season].Add(i);
}
foreach (long var in seasons.Keys)
{
//List<long> list = seasons[var];
//foreach (long t in list)
//{
Console.WriteLine(var);
//}
}
Dictionary<long, List<long>> seasons = new Dictionary<long, List<long>>();
for (long i = 201102; i <= 201108; i++)
{
long season = (i % 100 - 1) / 3 + 1;
if (!seasons.ContainsKey(season))
seasons.Add(season, new List<long>());
seasons[season].Add(i);
}
foreach ( KeyValuePair<long, List<long>> p in seasons)
{
Console.WriteLine(p.Key);
foreach (long date in p.Value)
Console.WriteLine(date);
}
void Main()
{
var _list =Enumerable.Range(201101,12);
var query=_list.GroupBy(l=>GetMonth(l));
int m=1;
var temp=query.ToDictionary(l=>m++);
}
int GetMonth(int time)
{
var temp=time.ToString();
var month=int.Parse(temp.Substring(4,2));
return month%4==0?(month/4)-1:month/4;
}