CodeForces 34C Page Numbers

来源:互联网 发布:淘宝抠图多少钱 编辑:程序博客网 时间:2024/05/29 17:08
Page Numbers
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 34C

Description

«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 Input

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
     没想到这还是个CF的C题为啥晚上做的时候感觉不出来这么好做,看样晚上不适合
做题啊...
      大体题意:输入一系列的数,如果有连续的数字则输出连续的区间例如
(1,2,3,4,5,6,就要写成1-6)最后输出用区间表示的数据。因为不知道什么时候输入
结束,所以用到Ctrl + z来终止。
AC代码
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;int main(){    int a[100010];    int b[100010];    while(scanf("%d",&a[0])!=EOF)    {        getchar();        int n = 1;        while(scanf("%d",&a[n])!=EOF)        {            n++;            getchar();        }        sort(a,a+n);        a[n] = 9999999;        int h = 0;        for(int i=0;i<n;i++)        {            if(i == 0)            {                b[h++] = a[i];            }            else if(a[i]!=a[i-1])            {                b[h++] = a[i];            }        }        b[h] = 99999;        int p = b[0];        int q = b[0];        int flag = 0;        int ff = 0;        for(int i=1;i<=h;i++)        {            if(b[i] == b[i-1]+1)            {                p = b[i];            }            else if(b[i]!=b[i-1]+1)            {                ff = 1;                if(flag == 1)                {                    printf(",");                }                flag = 1;                if(p == q)                {                    printf("%d",p);                }                else                {                    printf("%d-%d",q,p);                }                p = b[i];                q = b[i];            }        }       printf("\n");    }    return 0;}

0 0
原创粉丝点击