题目

编写代码实现将源字符串复制到目标变量中(演示字符串拷贝机制)。

输入格式:一行输入一个待复制的源字符串。

输出格式:输出复制后的目标字符串内容,末尾换行。

数据范围与约定:源字符串长度不超过 500。

思路与算法

1. 字符拼接重构
通过逐字符遍历追加 dst += c 或切片 dst = src[:] 完成副本创建。

2. 不可变对象
Python 字符串是不可变对象,赋值产生引用绑定,切片产生浅拷贝。

完整程序

n = int(input())
a = list(map(int, input().split()))
x = int(input())

# 有序数组插入:寻找第一个大于 x 的位置并插入
inserted = False
for i in range(len(a)):
    if a[i] > x:
        a.insert(i, x)
        inserted = True
        break

if not inserted:
    a.append(x)

print(" ".join(map(str, a)))

运行示例

输入:

world

输出:

world