c#将时间集合按照季度分组的算法

maniacstone 2011-05-09 11:54:12

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这样。


求算法,在线等!
...全文
475 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
maniacstone 2011-05-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 icedmilk 的回复:]
C# code
Dictionary<long, List<long>> seasons = new Dictionary<intlong List<long>>();
for (long i = 201102; i <= 201208; i++)
{
long season = (i ……
[/Quote]
long season = (i ……

这么算不对吧?在你你里能run么?
Icedmilk 2011-05-09
  • 打赏
  • 举报
回复
            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);
}
maniacstone 2011-05-09
  • 打赏
  • 举报
回复
我得到季度,是因为要做一个前后季度的数据对比
maniacstone 2011-05-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pmars 的回复:]
C# code

List<long> _list = new List<long>();
for(long i<=201102;i<=201208;i++)
{
_list.Add(i);
}


这段代码里面存有201113,这个是什么意思呢?
之后呢,我觉得,你如果想要按季度存储的话,还不如去些一个函数,传入一个月份值,之后出来的是季度!
里面一个switch就可以……
[/Quote]

是201103 年月 的格式 。
求实现代码。
pmars 2011-05-09
  • 打赏
  • 举报
回复

List<long> _list = new List<long>();
for(long i<=201102;i<=201208;i++)
{
_list.Add(i);
}

这段代码里面存有201113,这个是什么意思呢?
之后呢,我觉得,你如果想要按季度存储的话,还不如去些一个函数,传入一个月份值,之后出来的是季度!
里面一个switch就可以实现吧!!
a124819202 2011-05-09
  • 打赏
  • 举报
回复
给分吧
maniacstone 2011-05-09
  • 打赏
  • 举报
回复
这个规则有了变化,不好意思。分会继续给大家。
maniacstone 2011-05-09
  • 打赏
  • 举报
回复
对不住大家了,规则有问题。
q107770540 2011-05-09
  • 打赏
  • 举报
回复

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;
}
maniacstone 2011-05-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 icedmilk 的回复:]
刚才确实少了个+1

C# code

Dictionary<long, List<long>> seasons = new Dictionary<long, List<long>>();
for (long i = 201102; i <= 201108; i++)
{
long……
[/Quote]
我题目中表达有误。



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);
//}
}



这样可以明确看到结果。算法可能需要修改下。
Icedmilk 2011-05-09
  • 打赏
  • 举报
回复
刚才确实少了个+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);
}
q107770540 2011-05-09
  • 打赏
  • 举报
回复


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;
}
maniacstone 2011-05-09
  • 打赏
  • 举报
回复
我这里有地方写错了。
List<long> _list = new List<long>();
for(long i<=201102;i<=201208;i++)
{
_list.Add(i);
}
_list.Sort();

这里的意思仅仅是为了表达方便。
其实日期仅仅是每年的 年+月 而已,不会出现 201113 这种数据。

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧