CodeForces 34C Page Numbers
来源:互联网 发布:淘宝抠图多少钱 编辑:程序博客网 时间:2024/05/29 17:08
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
1,2,3,1,1,2,6,6,2
1-3,6
3,2,1
1-3
30,20,10
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;}
- CodeForces 34C Page Numbers
- CodeForces 34C Page Numbers
- CodeForces 34C Page Numbers
- Codeforces 34C Page Numbers(简单图论+DFS)
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
- Codeforces Beta Round #34 (Div. 2) C. Page Numbers
- CodeForces-630C. Lucky Numbers
- Codeforces 630C Lucky Numbers
- CodeForces-630C. Lucky Numbers
- CodeForces 630C:Lucky Numbers
- CodeForces - 630C Lucky Numbers
- 【CodeForces】[630C]Lucky Numbers
- Codeforces 630C Lucky Numbers
- codeforces 898C. Phone Numbers
- Codeforces 300 C Beautiful Numbers (Locas)
- codeforces 9C. Hexadecimal's Numbers
- codeforces 263C. Circle of Numbers
- CodeForces 300C Beautiful Numbers 组合数取余
- 谈谈初学者该怎么学电脑
- 集合与多重集合(set和multiset)
- js获取div的坐标
- WIFI破解之胡言乱语
- UVALive 6198 A Terribly Grimm Problem 二分匹配 求字典序最小解
- CodeForces 34C Page Numbers
- Xcode设置项之Architectures和Valid Architectures
- hibernate简单实例
- 映射和多重映射(map和multimap)
- String Date Calendar Timestamp之间的转换
- hdu 5092 Seam Carving 2014上海全国邀请赛——题目重现
- 遇到问题了。。。
- Javascript 基础知识 - 数组
- CodeForces 34B Sale