• 杭电1062-字符串翻转


    这是天津大学2015考研的编程题

    Problem Description
    Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
     
    Input
    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
    Each test case contains a single line with several words. There will be at most 1000 characters in a line.
     
    Output
    For each test case, you should output the text which is processed.
     
    Sample Input
    3 olleh !dlrow
    m'I morf .udh
    I ekil .mca
     
    Sample Output
    hello world!
    I'm from hdu.
    I like acm.
    Hint
    Remember to use getchar() to read ' ' after the interger T, then you may use gets() to read a line and process it.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main()
    {
        char c,buffer[1001];
        char* text_end;//每段字符串的结束地址
        char* text_begin;//每段字符串的开始地址
        int i=0,j=0;
        int cnt;
        char  temp;
     
        scanf("%d",&cnt);
        while(getchar()!=' ');//输入case数目的时候错了,你只读最后一个字符。。13你会读成3。。
        while(i<cnt)
        {
            gets(buffer);
            strcat(buffer," ");//为了方便输出,将输入的字符添加空格和字符串结束符
            text_begin=&buffer[0];//初始化第一段字符串的起始地址
            for(j=0; j<strlen(buffer); j++)
            {
                if(buffer[j]==' ')//遇到空格时,将字符串翻转
                {
                    text_end=&buffer[j-1];//字符串的结束地址
                    while(text_begin<text_end)//字符串翻转
                    {
                        temp=*text_begin;
                        *text_begin=*text_end;
                        *text_end=temp;
                        text_begin++;
                        if(text_begin==text_end) break;
                        text_end--;
                    }
                    text_begin=&buffer[j+1];//每段字符串结束重置开始地址
                }
            }
            buffer[strlen(buffer)-1]='';//删除结尾字符串的空格字符
            printf("%s ",buffer);
            i++;
        }
        return 0;
    }

    另外看到一个解法:(用栈实现)

    #include <iostream>
    #include <cstring>
    #include <stack>
    using namespace std;
    int main()
    {
        int T;
        cin >> T;
        while(T --)
        {
            stack<char> ch_stk;        //思路是一个个单词扔栈里面再弹出就反转了
            string ori_msg;
            string ch_msg;
            getline(cin, ori_msg);
            ch_msg.resize(ori_msg.length());
            int j = 0;
            for(int i=0; i<ori_msg.length(); i++)
            {
                if(ori_msg[i] != ' ')              //
                    ch_stk.push(ori_msg[i]);
                else
                {
                    while(!ch_stk.empty())
                    {
                        ch_msg[j++] = ch_stk.top();
                        ch_stk.pop();
                    }
                   // if(i != ori_msg.length()-1)          //句子原来的空格扔回去
                    ch_msg[j++] = ' ';
                }
            }
            while(!ch_stk.empty())                  //处理最后一个单词,因为这里已经跳出循环了
            {
                ch_msg[j++] = ch_stk.top();
                ch_stk.pop();
            }
            cout << ch_msg << endl;
        }
        return 0;
    }

    一个精简版本:

    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        char str[1000]={''};
        int n;
        cin>>n;
        getchar();
        while(n--)
        {
            cin.getline(str,1000);
            int slen=strlen(str);
            int flag=0;
            int i,j;
            for(i=0;i<strlen(str);i++)
            {
                if(str[i]==' ')
                {
                    for(j=i-1;j>=flag;j--)
                    {
                        cout<<str[j];
                    }
                    cout<<" ";
                    flag=i+1;
                }
            }
             for(j=i-1;j>=flag;j--)
             {
                cout<<str[j];
             }
             cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    移动端适配原理浅析
    springMVC框架下——通用接口之图片上传接口
    Nginx服务器
    sql sever和mysql 卸载及oracle安装
    高清图片爬取
    中国部分大学排名爬虫
    selenium和PhantomJS的安装
    NYOJ2—括号配对问题
    爬虫之小说爬取
    数据库系统概论书本基础知识
  • 原文地址:https://www.cnblogs.com/wft1990/p/4323272.html
Copyright © 2020-2023  润新知