C. Page Numbers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

«Bersoft» company is working on a new version of its most popular text editor — Bord 2010. Bord, like many other text editors, should be able to print out multipage documents. A user keys a sequence of the document page numbers that he wants to print out (separates
them with a comma, without spaces).

Your task is to write a part of the program, responsible for «standardization» of this sequence. Your program gets the sequence, keyed by the user, as input. The program should output this sequence in format l1-r1,l2-r2,...,lk-rk,
where ri + 1 < li + 1 for
all i from 1 to k - 1,
and li ≤ ri.
The new sequence should contain all the page numbers, keyed by the user, and nothing else. If some page number appears in the input sequence several times, its appearances, starting from the second one, should be ignored. If for some element i from
the new sequence li = ri,
this element should be output as li,
and not as «li - li».

For example, sequence 1,2,3,1,1,2,6,6,2 should be output as 1-3,6.

Input

The only line contains the sequence, keyed by the user. The sequence contains at least one and at most 100 positive integer numbers. It's guaranteed, that this sequence consists of positive integer numbers, not exceeding 1000, separated with a comma, doesn't
contain any other characters, apart from digits and commas, can't end with a comma, and the numbers don't contain leading zeroes. Also it doesn't start with a comma or contain more than one comma in a row.

Output

Output the sequence in the required format.

Sample test(s)
input
1,2,3,1,1,2,6,6,2
output
1-3,6
input
3,2,1
output
1-3
input
30,20,10
output
10,20,30
题意有点坑。

。看了半小时才看懂
题意:给一串数字,排序去重后,连续的要合并,比方 1,2,3 变成1-3。 而不连续的,比方1,2,3,6,   6要单独打印。即 1-3。6
输入不好弄。。

然后我直接串输入又瞎处理了一番。。后来去重+排序是直接扔到set里面撸一遍然后在塞到vector里了QAQ
在然后就是依据题意乱搞了
代码写挫了。。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 116
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
char str[10100];
int main()
{ while(~scanf("%s",str)){
set <int> s;int tem;char sb[6];
for(int i=0;i<=strlen(str);i++)
{
if(str[i]==','||str[i]=='\0')
{
int p=0,q=i;
while((--q)>=0&&str[q]!=',')sb[p++]=str[q];
sb[p]='\0';strrev(sb);sscanf(sb,"%d",&tem);
s.insert(tem);
}
}
vector <int> a(s.begin(),s.end());
int ansx[1010],ansy[1010],p=0;
for(int i=0;i<a.size()-1;i++)
{
int pos=i;
while(i+1<a.size()&&a[i]+1==a[i+1])
i++;
ansx[p]=a[pos];ansy[p++]=a[i];
}
if(ansy[p-1]!=a[a.size()-1])
{
ansx[p]=a[a.size()-1];
ansy[p++]=a[a.size()-1];
}
for(int i=0;i<p;i++)
{
if(i!=p-1)
{
if(ansx[i]==ansy[i])
printf("%d,",ansx[i]);
else
printf("%d-%d,",ansx[i],ansy[i]);
}
else
{
if(ansx[i]==ansy[i])
printf("%d\n",ansx[i]);
else
printf("%d-%d\n",ansx[i],ansy[i]);
}
}
}
return 0;
}

最新文章

  1. ArcGIS Engine开发之地图基本操作(1)
  2. 插入排序 - C语言
  3. Java api 入门教程 之 JAVA的文件操作
  4. ural 1208 Legendary Teams Contest
  5. JMeter的定时器
  6. 微信支付开发,再次签名,APP调用
  7. Android 应用自动更新功能的代码
  8. 【转】关于Ubuntu的sources.list 的总结
  9. Uva 12569 Planning mobile robot on Tree (EASY Version)
  10. Xutils呼叫流源代码文件下载方法
  11. springmvc与freemarker的整合
  12. 泛型约束new()的使用
  13. python单列模式
  14. CSS面试题
  15. You must have a copy of the scp binary locally to use the scp feature
  16. cocos2d-Lua02Lua面向对象
  17. css 初始包含块
  18. [转载]python的常用代码模板
  19. Kubernetes集群中Service的滚动更新
  20. AndroidManifest.xml权限设置

热门文章

  1. JavaScript基础之原型对象和原型链
  2. Python与数据结构[0] -&gt; 链表/LinkedList[2] -&gt; 链表有环与链表相交判断的 Python 实现
  3. 扫面线+线段树(hdu1542)
  4. [BZOJ 1037] 生日聚会Party
  5. 【bzoj2190】【仪仗队】欧拉函数+线性筛(浅尝ACM-J)
  6. [JSOI2017]原力
  7. java随机生成汉字
  8. 咏南下拉列表非数据敏感控件--TYNSearch
  9. Unix网络编程学习笔记之第12章 IPv4与IPv6的互操作性
  10. 转: 苹果APNS的说明