1 条题解

  • 0
    @ 2025-4-14 18:43:48

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    	char a[1000],b[1000]={NULL};
    	gets(a);
    	int len=strlen(a);
    	a[len]=' ';
    	a[len+1]='\0';
    	for(int i=len-1;i>=0;i--)
    	{
    		if(a[i]==' ')
    		{
    			strcat(b,a+i+1);
    			a[i+1]='\0';
    		}
    	}
    	strcat(b,a);
    	puts(b);
    }
    

    C++ :

    #include <stdio.h>
    #include <stdlib.h>
    #include<iostream>
    #include <cstring>
    using namespace std;
    
    void ReverseWord(char* p, char* q)
    {
        while(p < q)
        {
            char t = *p ;
            *p++ = *q ;
            *q-- = t ;
    	}
    }
    char* ReverseSentence(char* s)
    {
        char* p = s ;
    	char* q = s ;
    
        while(*q != '\0')
        {
            if (*q == ' ')
            {
                ReverseWord(p, q - 1) ;
                q++ ; 
                p = q ;
            }
            else
                q++ ;
    	}
        ReverseWord(p, q - 1) ; 
        ReverseWord(s, q - 1) ; 
        return s ;
    }
    int main(){
    	char a[2001];
    	gets(a);
    	printf("%s\n",ReverseSentence(a));
    	
    	return 0;
    }
    
    

    Python :

    # coding=utf-8
    s=input()
    lst=s.split()
    for i in range(len(lst),0,-1):
        print(lst[i-1],end="")
        if i!=1:
            print(" ",end="")
    
    • 1

    信息

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