35、创建一个包含至少六个整数的数组。复制该数组,并更新复制后的数组,使其中每隔一个的值更新为其前一个值。例如,如果原始数组是 [7 5 10 15 30 50],则复制后的数组最终版本应为 [7 7 10 10 30 30]。不要对原始数组做任何更改。
以下是一个满足该需求的 Go 语言示例代码:
package main
import "fmt"
func main() {
// 创建包含至少六个整数的原始数组
originalArray := [6]int{7, 5, 10, 15, 30, 50}
// 复制数组
newArray := originalArray
// 更新复制后的数组,使每隔一个的值更新为其前一个值
for i := 1; i < len(newArray); i += 2 {
newArray[i] = newArray[i-1]
}
// 输出原始数组和更新后的数组
fmt.Println("原始数组:", originalArray)
fmt.Println("更新后的数组:", newArray)
}
此代码首先创建了一个包含六个整数的原始数组,然后复制该数组。接着,使用 for 循环遍历复制后的数组,将奇数索引位置的值更新为其前一个值。最后,输出原始数组和更新后的数组,确保原始数组未被更改。
36、创建一个程序,执行以下任务:提示用户回答一系列关于他们自己的 5 - 10 个问题,例如他们的姓名、生日、居住地点、最喜欢的爱好/运动等。至少包含 10 个问题,但在用户回答至少 5 个问题后,给用户退出的选项。将答案保存到一个数组中。以用户友好的格式向用户显示结果。例如,如果其中一个问题是“你叫什么名字?”,该响应的输出应该是“你的名字是玛丽”。显示所有答案后,提示用户更改一个或多个答案。用新答案更新数组并重新显示结果。允许用户在任何时候退出程序。
以下是一个 Python 示例代码来实现该程序:
questions = [
"What is your name?",
"When is your birthday?",
"Where do you live?",
"What is your favorite hobby?",
"What is your favorite sport?",
"Do you have any pets?",
"What is your favorite color?",
"What is your favorite food?",
"What is your occupation?",
"What is your dream job?"
]
answers = []
for i, question in enumerate(questions):
if i >= 5:
quit_option = input("Do you want to quit? (yes/no): ").lower()
if quit_option == "yes":
break
answer = input(question + " ")
answers.append(answer)
for i, question in enumerate(questions[:len(answers)]):
print(f"{question[:-1].replace('What is', 'Your')} is {answers[i]}.")
while True:
change_option = input("Do you want to change any answers? (yes/no): ").lower()
if change_option == "no":
break
index = int(input("Enter the index of the answer you want to change (starting from 0): "))
if 0 <= index < len(answers):
new_answer = input(f"Enter the new answer for '{questions[index]}': ")
answers[index] = new_answer
for i, question in enumerate(questions[:len(answers)]):
print(f"{question[:-1].replace('What is', 'Your')} is {answers[i]}.")
else:
print("Invalid index. Please try again.")
这段代码首先定义了一系列问题,然后提示用户回答这些问题,在用户回答至少 5 个问题后提供退出选项。答案被保存到 answers 数组中,之后以友好的格式显示结果。最后,允许用户更改答案并重新显示更新后的结果。
37、创建一个包含三个变量的程序:姓名、年龄和性别。为每个变量赋值,使这三个变量的数据类型各不相同。创建指向每个变量的指针。使用fmt.Println语句,既通过原始变量,也通过解引用指针来打印这三个变量的值。
以下是一个满

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



