返回信息流Write a C program, named listints, which will simply print the integers requested on its command-line. The output is to appear in strictly increasing order, each requested integer appearing once and only once. Typical examples of its use include:
listints 3,5,9 ( output should be 3 5 9)
listints 1-10 (output should be 1 2 3 ...10)
listints 1-10,6 (output is the same with the above line)
listints 2000-2020,40-50 (output omited, give the reasonable output yourself!)
listints 1-10,2010-2020,300000-300010
这道题好在就一个argument,但怎么把里面的数字抽取出来,删除重复数字并进行排序呢?我倒是有比较笨的方法从字符串开头一点点判断,但写下来有很多分支。有没有比较简单的方法?请大牛指教。
这是一条镜像帖。来源:北邮人论坛 / cpp / #18850同步于 2009/1/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
依然是字符串问题。。。
wwang
2009/1/23镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
这应该发到算法版
我觉得不能展开数字区间,而是维护一个区间链表,链表的每个节点存储区间的起始和结束元素,整个链表递增排列,比如
3,5,9
head -> (3,3) -> (5,5) -> (9,9)
1-10
head -> (1,10)
2000,2020 40,50
head ->(40,50) -> (2000,2020)
剩下的问题就是怎么维护这个表
有序这个问题插入的时候就能解决
去重也不麻烦, 判断以下就行了
最后按链表依次输出就行
直接数组效率巨低,如果是类似这样的数据:
1-100000,2-100001,3-100002,那复杂度就太高了
链表是对数组的一个优化,线段树是对链表的进一步优化
To jokerlee,wks,PtwCJ.
Many thanks!
Seems I lack basic knowledge of algorithm and data structure.