CodeForces 34C Page Numbers
来源:互联网 发布:手机淘宝网上怎么直播 编辑:程序博客网 时间:2024/06/08 01:02
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 组合数取余
- 167 请编程实现全排列算法
- 【leetcode 层序遍历】Binary Tree Level Order Traversal
- 数据结构二叉树的前序中序后序层次遍历简单版,手写
- PHP 随机生成唯一的ID
- [BZOJ 2150]部落战争(最小路径覆盖、二分图最大匹配)
- CodeForces 34C Page Numbers
- 厦门大学线下编程比赛第一题:求和
- 链表(list)
- C语言内存管理-字符数组与字符指针
- D3DPRESENT_PARAMETERS结构
- 大家注意,A股牛市来了
- linux环境下使用minicom控制开发板
- 面向对象和面向过程的区别
- Qt Quick实现的涂鸦程序