2 solutions

  • 1
    @ 2026-6-23 20:09:22
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	string s;
    	getline(cin,s);
    	int n=s.size();
    	for(int i=0;i<n;i++)
    	{
    		if(s[i]==' ') //特殊情况判定 
    		{
    			cout<<" ";
    		}
    		else
    		{
    			int j=i;
    			while(j<n&&s[j]!=' ') j++;
    			j--;
    			//i是第一个字母的位置,j是最后一个字母的位置
    			string s1=s.substr(i,j-i+1);
    			reverse(s1.begin(),s1.end());//翻转
    			cout<<s1;	
    			i=j;//跳到最后一个位置 
    		}
    
    	}
      return 0;
    }
    
    
    • 0
      @ 2026-11-19 17:39:54
      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
      	string s;
      	getline(cin,s);
      	int n=s.size();
      	s+=" "; 
      	int last=0;
      	for(int i=0;i<=n;i++)
      	{
      		if(s[i]==' ')
      		{
      			for(int j=i-1;j>=last;j--)
      			{
      				cout<<s[j];
      			}
      			last=i+1;
      			cout<<" ";
      		}
      	}
      	return 0;
      } 
      
      • 1

      Information

      ID
      922
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      1
      Tags
      (None)
      # Submissions
      131
      Accepted
      50
      Uploaded By