Codeforces 34C-Page Numbers(set+vector+暴力乱搞)

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]=='') { int p=0,q=i; while((--q)>=0&&str[q]!=',')sb[p++]=str[q]; sb[p]='';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 ",ansx[i]); else printf("%d-%d ",ansx[i],ansy[i]); } } } return 0; }

原文地址:https://www.cnblogs.com/jhcelue/p/7259918.html