Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
来源:互联网 发布:杜绝 流氓 抓取数据 编辑:程序博客网 时间:2024/05/23 19:39
«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.
1,2,3,1,1,2,6,6,2
1-3,6
3,2,1
1-3
30,20,10
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]=='\0'){int p=0,q=i;while((--q)>=0&&str[q]!=',')sb[p++]=str[q];sb[p]='\0';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]);elseprintf("%d-%d,",ansx[i],ansy[i]);}else{if(ansx[i]==ansy[i])printf("%d\n",ansx[i]);elseprintf("%d-%d\n",ansx[i],ansy[i]);}} }return 0;}
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
- CodeForces 34C Page Numbers
- CodeForces 34C Page Numbers
- CodeForces 34C Page Numbers
- Codeforces 34C Page Numbers(简单图论+DFS)
- Codeforces Beta Round #34 (Div. 2) C. Page Numbers
- CodeForces 723C-Polycarp at the Radio(模拟 贪心 vector乱搞)
- Codeforces Round #207 (Div. 2)---C. Knight Tournament(set乱搞)
- codeforces#331-C - Wilbur and Points-set/贪心乱搞
- Codeforces 675c 乱搞
- Codeforces #274 (Div. 2) (数据结构:set乱搞)
- CodeForces 830B Cards Sorting(set乱搞)
- CodeForces 732D Exams (思维+set乱搞)
- Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(暴力)
- Codeforces 300C Beautiful Numbers 组合数取模+暴力
- Codeforces Round #340 -C - Watering Flowers-暴力+set
- CodeForces-485C Bits(乱搞)
- Problem 41 Pandigital prime (暴力set+vector)
- 链表
- 猜大小游戏
- Java I/O操作汇总
- 一个可以说话的人
- Servlet 学习笔记(一)
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
- 一个硬件创业者的自述:我这20年
- 1088
- mysql 中的varchar(255) 能放多少汉字
- 让SecureCRT显示光标所在处(光标闪烁)
- <一>Oracle11g的安装+数据库创建+PL/SQL Dev的使用初步
- OpenCV入门(二十八) -- 多边形逼近
- [AHK]获取多个文件路径,构造命令行,发送给Beyond Compare等程序
- <Android>关于获取资源文件R中的数据