[codeforces]Page Numbers <模拟>

描述:

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

输入:

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 the sequence in the required format.

 

题意基本就是思路

把所有数字去重排序,挨在一起的记录下来输出,没挨在一起单个输出就行

没什么难度

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<utility>
#include<stack>
#include<cstdlib>
#define ll long long
#define inf 0x3fffffff
using namespace std;

int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

void fre(){
     freopen("     .in","r",stdin);
     freopen("     .out","w",stdout);
}

int n,flag,num1,num2;
int a[100005],barrel[100005],final[100005],tot;

int main(){
    while(scanf("%d",&a[n++])!=EOF)
        char ch=getchar();
    for(int i=0;i<=n;i++){
        if(barrel[a[i]]==0){
            final[tot]=a[i];tot++;
        }barrel[a[i]]++;
    }
    sort(final,final+tot-1);
    num1=num2=final[0];
    flag=0;
    for(int i=1;i<tot;i++){
        if(final[i]==final[i-1]+1)num1=final[i];
        else if(final[i]!=final[i-1]+1){
            if(flag)printf(",");
            flag=1;
            if(num1==num2)printf("%d",num1);
            else printf("%d-%d",num2,num1);
            num1=final[i];
            num2=final[i];
        }
    }
    return 0;
}
View Code

 

 

posted @ 2020-02-12 22:26  Danzel♂  阅读(198)  评论(0编辑  收藏  举报