1.7 编程基础之字符串 27 单词翻转 4分 python

"""
1.7 编程基础之字符串 27 单词翻转 4分
http://noi.openjudge.cn/ch0107/24/
https://blog.csdn.net/hy_696/article/details/106412888
https://blog.csdn.net/m0_43456002/article/details/102729230

LeetCode--最后一个单词的长度(python版)
https://www.jianshu.com/p/e45152efc507

python反转list的三种方法
https://blog.csdn.net/qq_37969201/article/details/80406726

"""
a=input().split()
a=list(a)
lena=len(a)
i=0
while i<lena-1:

            #print(str(list(reversed(a[i])))+",",end=' ')

            print( str(a[i][: :-1]),end=' ')

            i+=1

print( str(a[lena-1][: :-1]))

C++代码:

/*
1.7编程基础之字符串_27单词翻转04
http://noi.openjudge.cn/ch0107/27/
*/
#include<iostream>
#include<cstring>
#include<string>
using namespace std;

string a;

void Reverse(int lft,int r)
{
	char x;
	for(int i=lft,j=r;i<j;i++,j--)
	{
		x=a[i];
		a[i]=a[j];
		a[j]=x;
	}
}

int main()
{
	int n,i;
	
	getline(cin,a);
	
	int lena=0;
	
	for(i=0;i<a.length();i++)
	{
		if(a[i]==' ')
		{
			Reverse(lena,i-1);
			lena=i+1; 
		}
		
		if(i==a.length()-1)
		{
			Reverse(lena,i);
			lena=i+1;
		}
	}
	
	cout<<a<<endl;
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dllglvzhenfeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值