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); }
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; }
刚才确实少了个+1 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); }
我题目中表达有误。 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); //} }这样可以明确看到结果。算法可能需要修改下。
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()))));
List<long> _list = new List<long>();
for(long i<=201102;i<=201208;i++)
{
_list.Add(i);
}这段代码里面存有201113,这个是什么意思呢?
之后呢,我觉得,你如果想要按季度存储的话,还不如去些一个函数,传入一个月份值,之后出来的是季度!
里面一个switch就可以实现吧!!
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);
}
_list.Sort();这里的意思仅仅是为了表达方便。
其实日期仅仅是每年的 年+月 而已,不会出现 201113 这种数据。
{
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;
}
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);
}
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);
//}
}这样可以明确看到结果。算法可能需要修改下。
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;
}