BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #18850同步于 2009/1/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

依然是字符串问题。。。

wwang
2009/1/23镜像同步7 回复
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,但怎么把里面的数字抽取出来,删除重复数字并进行排序呢?我倒是有比较笨的方法从字符串开头一点点判断,但写下来有很多分支。有没有比较简单的方法?请大牛指教。
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
jokerlee机器人#1 · 2009/1/23
这应该发到算法版 我觉得不能展开数字区间,而是维护一个区间链表,链表的每个节点存储区间的起始和结束元素,整个链表递增排列,比如 3,5,9 head -> (3,3) -> (5,5) -> (9,9) 1-10 head -> (1,10) 2000,2020 40,50 head ->(40,50) -> (2000,2020) 剩下的问题就是怎么维护这个表 有序这个问题插入的时候就能解决 去重也不麻烦, 判断以下就行了 最后按链表依次输出就行
PtwCJ机器人#2 · 2009/1/23
数组标记阿,处理就两种情况,逗号和杠
jokerlee机器人#3 · 2009/1/23
数字范围大的话,数组更本开不了那么大
PtwCJ机器人#4 · 2009/1/23
开成全局的 再不行动态分配 【 在 jokerlee 的大作中提到: 】 : 数字范围大的话,数组更本开不了那么大
wks机器人#5 · 2009/1/23
线段树试试看
jokerlee机器人#6 · 2009/1/23
直接数组效率巨低,如果是类似这样的数据: 1-100000,2-100001,3-100002,那复杂度就太高了 链表是对数组的一个优化,线段树是对链表的进一步优化
wwang机器人#7 · 2009/1/23
To jokerlee,wks,PtwCJ. Many thanks! Seems I lack basic knowledge of algorithm and data structure.