zoukankan      html  css  js  c++  java
  • 2017.7.24

    整数分割 

    Broken Keyboard (a.k.a. Beiju Text)

    Youre typing a long text with a broken keyboard. Well its not so badly broken. The only problem

    with the keyboard is that sometimes the homekey or the endkey gets automatically pressed

    (internally).

    Youre not aware of this issue, since youre 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 Homekey is pressed

    internally, and ]means the Endkey 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;  

  • 相关阅读:
    docker配置国内加速器
    pwm原理与stm32控制
    【C语言】赋值表达式自增自减逗号表达式
    linux下利用paho.mqtt.c进行mqtt开发 #c编程
    linux下tcp #c编程
    windows下的linux子系统虚拟串口的使用
    linux下搭建mqtt服务器(mosquitto)
    linux下paho.mqtt.c下载安装
    树莓派&mysql数据库
    vscode+mysql数据库编程
  • 原文地址:https://www.cnblogs.com/l-w-j/p/7231052.html
Copyright © 2011-2022 走看看