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;  

  • 相关阅读:
    js里面的 InttoStr 和 StrtoInt
    预编译知识 (转载记录)
    C语言操作内存
    C语言操作文件
    C语言
    如何调试shell脚本
    设计模式-装饰者模式
    自己动手制作一个模版解析
    设计模式-单例模式
    http中关于缓存的那些header信息
  • 原文地址:https://www.cnblogs.com/l-w-j/p/7231052.html
Copyright © 2011-2022 走看看