zoukankan      html  css  js  c++  java
  • 杭电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;
    }
  • 相关阅读:
    RMI方式Ehcache集群的源码分析
    Bootstrap简单HelloWorld入门教程
    深入剖析Tomcat会话机制
    Redis主从和HA配置
    Ajax
    java数组复制===clone()
    java使用array.copy复制数组
    java数组复制的简单方法(一)
    java ------------用Array.sort()输出数组元素的最大值,最小值,平均值~~~~
    java从键盘输入若干数,求其最大值,最小值,平均值。等等
  • 原文地址:https://www.cnblogs.com/wft1990/p/4323272.html
Copyright © 2011-2022 走看看