python利用循环改变数组内容_Python程序将数组循环旋转一个

给定用户输入数组。我们的任务是循环旋转意味着顺时针旋转该值。

示例Input: A=[1,2,3,4,5]

Output=[5,1,2,3,4]

算法Step 1: input array element.

Step 2: Store the last element in a variable say x.

Step 3: Shift all elements one position ahead.

Step 4: Replace first element of array with x.

范例程式码# Python program to cyclically rotate

#an array by one

# Method for rotation

def rotate(A, n):

x = A[n - 1]

for i in range(n - 1, 0, -1):

A[i] = A[i - 1];

A[0] = x;

# Driver function

A=list()

n=int(input("Enter the size of the List ::"))

print("Enter the Element of  List ::")

for i in range(int(n)):

k=int(input(""))

A.append(k)

print ("The array is ::>")

for i in range(0, n):

print (A[i], end = ' ')

rotate(A, n)

print ("\nRotated array is")

for i in range(0, n):

print (A[i], end = ' ')

输出结果Enter the size of the  List ::5

Enter the Element of  List ::

8

7

90

67

56

The array is ::>

8 7 90 67 56

Rotated array is

56 8 7 90 67

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值