整数分割
Broken Keyboard (a.k.a. Beiju Text)
You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem
with the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed
(internally).
You’re not aware of this issue, since you’re focusing on the text and did not even turn on the
monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).
In Chinese, we can call it Beiju. Your task is to find the Beiju text.
Input
There are several test cases. Each test case is a single line containing at least one and at most 100,000
letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed
internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file
(EOF).
Output
For each case, print the Beiju text on the screen.
Sample Input
This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University
Sample Output
BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University
思路:将字符转换成整数
首先用%c储存输入的字符a[i],用b[i]来储存不是5的数字,当遇到5时,b[i]数组的下标加1
源代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char a[5005];
int b[5005];
int main()
{
while(scanf("%s",a)!=EOF)
{
memset(b,0,sizeof(b));//记住清零
int t=0;
int len=strlen(a);
int flag=0;
for(int i=0;i<len;i++)
{
if(a[i]!='5')//如果不是字符5,直接保存
{
b[t]=b[t]*10+(a[i]-'0');
flag=1;
}//并且这一句将前导5也给考虑到了,前导是5,直接往后遍历就行了!
else if(flag==1)//由于害怕有多个字符5相连会多次t++操作,所以要
{//用一个flag标志来表示这个五前面是否有不是字符5的数,如果有的话
t++;//才能说明需要再用一个容器来保存下一个数!
flag=0;//(也就是说排除多个5相连的情况) 2017-07-24
}//遇到字符5,就进行t++,除了多个5相连的情况,只需要加一次
if(a[i]!='5'&&a[i+1]==' ')//处理结尾不是字符5的情况!
{
t++;
}
}
sort(b,b+t);//从小到达排序
for(int i=0;i<t-1;i++)
{
printf("%d ",b[i]);
}
printf("%d ",b[t-1]);
}
return 0;
}