Codeforces Beta Round #34 (Div. 2)——C

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.

#include <cstdio>
#define maxn 1005
using namespace std;

char s[maxn];
bool cnt[maxn]={false};
int main()
{
    scanf("%s",s);
    int c=0;
    for(int i=0;s[i];i++)
    {
        while(s[i]!=','&&s[i]!='\0')
            {c=c*10+s[i]-'0';i++;}//读取数字
        cnt[c]=true;//存入数组
        c=0;
    }

    int j;
    for(j=1000;j>0;j--)
        if(cnt[j]) break;//找到出现的最大数字
    int i=1;
    while(i<=1000)
    {
        if(cnt[i])
        {
            printf("%d",i);
            int x=i;
            while(cnt[i]) i++;
            if(x!=i-1)
            {
              if(i!=j+1)  printf("-%d,",i-1);//如果是最大数字,不加逗号
              else printf("-%d",i-1);
            }
            else if(i!=j+1) printf(",");//如果是最大数字,不加逗号
        }
        i++;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值