1 条题解

  • 0
    @ 2025-4-12 21:52:30

    C :

    /*
     * =====================================================================================
     *
     *       Filename:  902-3.c
     *
     *    Description:  ihahah
     *
     *        Version:  1.0
     *        Created:  2013/9/3 0:32:01
     *       Revision:  none
     *       Compiler:  gcc
     *
     *         Author:  mdk-vim.cpp-c (mdk), mengdaikun@gmail.com
     *        Company:  cjluacm-vim-mdk
     *
     * =====================================================================================
     */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    char s[1001];
     
    void reverse(int begin,int end)
    {
        while(begin<end)
        {
            char temp=s[begin];
            s[begin]=s[end];
            s[end]=temp;
            ++begin;
            --end;
        }
    }
     
    int main()
    {
        //freopen("a.in","r",stdin);
        
        while(gets(s))
        {
            int len=strlen(s),begin=0;
            
            reverse(0,len-1);
            
            int i;
    
            for(i=0;i<len;i++)
            {
                if(s[i]!=' ')
                {
                    if(i>0&&s[i-1]==' ')
                        begin=i;
                    else if(i<len-1&&s[i+1]==' ' || i==len-1)
                        reverse(begin,i);
                }
            }
            
            printf("%s\n",s);
        }
        return 0;
    }
     
    
    
    

    C++ :

    #include <stdio.h>
    ///////////////////////////////////////////////////////////////////////
    // Reverse a string between two pointers
    // Input: pBegin - the begin pointer in a string
    //        pEnd   - the end pointer in a string
    ///////////////////////////////////////////////////////////////////////
    void Reverse(char *pBegin, char *pEnd)
    {
          if(pBegin == NULL || pEnd == NULL)
                return;
    
          while(pBegin < pEnd)
          {
                char temp = *pBegin;
                *pBegin = *pEnd;
                *pEnd = temp;
    
                pBegin ++, pEnd --;
          }
    }
    ///////////////////////////////////////////////////////////////////////
    // Reverse the word order in a sentence, but maintain the character
    // order inside a word
    // Input: pData - the sentence to be reversed
    ///////////////////////////////////////////////////////////////////////
    char* ReverseSentence(char *pData)
    {
          if(pData == NULL)
                return NULL;
    
          char *pBegin = pData;
          char *pEnd = pData;
    
          while(*pEnd != '\0')
                pEnd ++;
          pEnd--;
    
          // Reverse the whole sentence
          Reverse(pBegin, pEnd);
    
          // Reverse every word in the sentence
          pBegin = pEnd = pData;
          while(*pBegin != '\0')
          {
                if(*pBegin == ' ')
                {
                      pBegin ++;
                      pEnd ++;
                      continue;
                }
                // A word is between with pBegin and pEnd, reverse it
                else if(*pEnd == ' ' || *pEnd == '\0')
                {
                      Reverse(pBegin, --pEnd);
                      pBegin = ++pEnd;
                }
                else
                {
                      pEnd ++;
                }
          }
    
          return pData;
    }
    int main()
    {
    	char s[9999];
    	gets(s);
    	while(s[0]!=0)
    	{
    		printf("%s\n",ReverseSentence(s));
    		s[0]=0;
    		gets(s);
    	}
    	return 0;
    } 
    

    Pascal :

    program p2152;
    var st,s:ansistring;
        i,j,k,n:longint;
    begin
      while not eof do
       begin
         s:='';
         readln(st);
         k:=pos(' ',st);
         while k<>0 do
          begin
            s:=' '+copy(st,1,k-1)+s;
            delete(st,1,k);
            k:=pos(' ',st);
          end;
         if length(st)>0 then
          s:=st+s;
         writeln(s);
       end;
    end.
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner cin = new Scanner(System.in);
    		while(cin.hasNext())
    		{
    			String str = cin.nextLine();
    			revString(str);
    		}
    	}
    	
    	public static String revString(String str)
    	{
    		StringBuilder builder = new StringBuilder();
    		builder.append("begin ");
    		builder.append(str);
    		builder.append(" end");
    		String []subStrings = builder.toString().split(" ");
    		for(int i= subStrings.length -2 ;i>=2;i-- ){
    			System.out.print(subStrings[i]+" ");
    		}
    		System.out.println(subStrings[1]);
    		return null;
    	}
    
    }
    
    • 1

    信息

    ID
    2189
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者