PAT 1104 Sum of Number Segments python解法

本文解析了一道算法题目,要求计算给定正数序列的所有连续子序列(段)的总和。通过观察序列{0.1,0.2,0.3,0.4}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1104 Sum of Number Segments (20 分)
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) and (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 105​​ . The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:
For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:
4
0.1 0.2 0.3 0.4
Sample Output:
5.00

题意:给出一个序列,求出它的所有段总和(段:连续的子序列)。比如,给定序列{0.1,0.2,0.3,0.4},求出10个段:(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) 和 (0.4).然后求和: 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0,注意要保留2位小数,最终结果是5.00

解题思路:通过找规律,可以发现,0.1出现了4次,0.2出现了6次,0.3出现了6次,0.4出现了4次,结果就是0.1*4+0.2*6+0.3*6+0.4*4=5.00。如果扩展到n个数的序列,可以发现,第i个数出现了i*(n+1-i)次(如:0.1出现了1*4次,0.2出现了2*3次,0.3出现了3*2次,0.4出现了4*1次),因为索引从0开始,所以代码中是(n-i)*(i+1)次,接下来只需要求和就行了。

n = int(input())
l = list(map(float,input().split()))
s = 0
for i in range(len(l)):
        s += l[i]*(n-i)*(i+1)
print('%.2f'%s)
下面是一个使用Python编写的ROS Noetic中MoveIt的Sequence of Multiple Segments功能的示例代码: ```python #!/usr/bin/env python3 import rospy import moveit_commander import moveit_msgs.msg def main(): # 初始化ROS节点 rospy.init_node('moveit_sequence_of_multiple_segments') # 初始化MoveIt moveit_commander.roscpp_initialize(sys.argv) # 创建机器人运动规划的对象 robot = moveit_commander.RobotCommander() # 创建机械臂运动规划的对象 arm = moveit_commander.MoveGroupCommander('arm') # 设置机械臂的允许误差值 arm.set_goal_joint_tolerance(0.001) # 创建轨迹规划的对象 trajectory = moveit_msgs.msg.RobotTrajectory() # 创建路径点的对象 waypoints = [] # 添加起点 start_pose = arm.get_current_pose().pose waypoints.append(start_pose) # 添加第一个中间点 mid_pose1 = start_pose mid_pose1.position.x += 0.1 waypoints.append(mid_pose1) # 添加第二个中间点 mid_pose2 = mid_pose1 mid_pose2.position.y -= 0.1 waypoints.append(mid_pose2) # 添加终点 end_pose = start_pose end_pose.position.x += 0.2 end_pose.position.y -= 0.2 waypoints.append(end_pose) # 设置路径点 (plan, fraction) = arm.compute_cartesian_path( waypoints, # 路径点 0.01, # eef_step 0.0, # jump_threshold True) # avoid_collisions # 将路径点添加到轨迹中 trajectory.joint_trajectory.points = plan.joint_trajectory.points # 创建Sequence of Multiple Segments消息 sequence = moveit_msgs.msg.MultiDOFJointTrajectory() # 设置时间 sequence.header.stamp = rospy.Time.now() # 设置关节名称 sequence.joint_names = trajectory.joint_trajectory.joint_names # 设置每个段的时间和路径点 for i in range(len(trajectory.joint_trajectory.points)): point = trajectory.joint_trajectory.points[i] segment = moveit_msgs.msg.MultiDOFJointTrajectoryPoint() # 设置时间 segment.time_from_start = point.time_from_start # 设置路径点 for j in range(len(point.positions)): dof = moveit_msgs.msg.MultiDOFJointTrajectoryPoint() dof.transforms.append(moveit_msgs.msg.Transform( translation=moveit_msgs.msg.Vector3(point.positions[j], 0, 0), rotation=moveit_msgs.msg.Quaternion(0, 0, 0, 1))) segment.transforms.append(dof) # 添加段 sequence.points.append(segment) # 发布Sequence of Multiple Segments消息 pub = rospy.Publisher('/move_group/display_planned_path', moveit_msgs.msg.DisplayTrajectory, queue_size=10) display_trajectory = moveit_msgs.msg.DisplayTrajectory() display_trajectory.trajectory.append(trajectory) pub.publish(display_trajectory) # 清除MoveIt的数据 moveit_commander.roscpp_shutdown() if __name__ == '__main__': try: main() except rospy.ROSInterruptException: pass ``` 这个示例代码演示了如何使用MoveIt的Sequence of Multiple Segments功能,将机器人运动规划成一系列的段,并依次执行这些段。代码中使用了ROS的Publisher来发布Sequence of Multiple Segments消息,以便在RViz中显示机器人的运动轨迹。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

D_ry

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

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

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

打赏作者

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

抵扣说明:

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

余额充值