Codeforces Beta Round #34 (Div. 2)——C
来源:互联网 发布:头像p图软件 编辑:程序博客网 时间:2024/04/29 14:29
«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>#define maxn 1005using 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;}
- Codeforces Beta Round #34 (Div. 2)——C
- Codeforces Beta Round #16 (Div. 2 Only)——C
- Codeforces Beta Round #18 (Div. 2 Only)——C
- Codeforces Beta Round #49 (Div. 2)——C
- Codeforces Beta Round #74 (Div. 2 Only)——C
- Codeforces Beta Round #34 (Div. 2) C题解
- Codeforces Beta Round #34 (Div. 2) C. Page Numbers
- Codeforces Beta Round #31 (Div. 2, Codeforces format)——C
- Codeforces Beta Round #65 (Div. 2)——A,B,C
- Codeforces Beta Round #67 (Div. 2)——A,B,C
- Codeforces Beta Round #70 (Div. 2)——A,B,C
- Codeforces Beta Round #72 (Div. 2 Only)——A,B,C
- Codeforces Beta Round #73 (Div. 2 Only)——A,B,C
- Codeforces Beta Round #76 (Div. 2 Only)——A,B,C
- Codeforces Beta Round #77 (Div. 2 Only)——A,B,C
- Codeforces Beta Round #96 (Div. 2)——B
- Codeforces Beta Round #93 (Div. 2 Only)——B
- Codeforces Beta Round #91 (Div. 2 Only)——B
- 解决方法:远程计算机需要网络级别身份验证,而您的计算机不支持该验证
- linux 终端常用命令
- 一些有用的网站
- centos 64 位系统安装postgresql odbc 方法
- 设置ubuntu Android sdk环境变量
- Codeforces Beta Round #34 (Div. 2)——C
- 简要分析蓝牙驱动
- 在C++中实现C#的委托
- jpa的context.xml文件
- iPhone-NSAssert使用
- 百度地图SDK for Android【事件监听】
- VC++实现解析快捷方式
- System.nanoTime (计时工具类)
- 记录我的问题与解决办法_链接