1、 广场上有一队士兵,如果排成10列纵队,最后一行只有9人;如果排成9列纵队,最后一行只有8人;如果排成8列纵队,最后一行只有7人……最后排成2列纵队,最后一行只有1人,编写程序,计算广场上士兵的最少人数,并输出(分别用蛮力法和数学建模方法求解)。
时间: 2023-10-22 10:07:44 浏览: 112
蛮力法:
```python
def brute_force():
n = 10 # 最开始的列数
while True:
for i in range(n-1, 1, -1):
if (n * (n+1)) % (i+1) == 0: # 判断是否符合条件
return n * (n+1) // (i+1) # 返回最少人数
n += 1 # 如果不符合条件,列数加1
print(brute_force()) # 输出结果
```
数学建模法:
设士兵的总人数为 $x$,则有:
$$\begin{aligned}
x &\equiv 9 \pmod{10} \\
x &\equiv 8 \pmod{9} \\
x &\equiv 7 \pmod{8} \\
&\cdots \\
x &\equiv 1 \pmod{2}
\end{aligned}$$
根据中国剩余定理,可以将上述同余方程组转化为一个方程:
$$x = 2520k + 1$$
其中 $k$ 是任意整数。因为要求 $x$ 最小,所以 $k=1$,即最少人数为 $2521$。
代码实现:
```python
def math_model():
return 2521
print(math_model()) # 输出结果
```
相关问题
广场上有一队士兵,如果排成10列纵队,最后一行只有9人;如果排成9列纵队,最后一行只有8人;如果排成8列纵队,最后一行只有7人……最后排成2列纵队,最后一行只有1人,编写程序,计算广场上士兵的最少人数,并输出
这是一个数学问题,可以使用中国剩余定理来求解。
根据题意,可以列出如下同余方程组:
```
x ≡ 9 (mod 10)
x ≡ 8 (mod 9)
x ≡ 7 (mod 8)
...
x ≡ 1 (mod 2)
```
其中 `x` 表示士兵的总数。
根据中国剩余定理,上述同余方程组的解可以表示为:
```
x = a1 * m1 * y1 + a2 * m2 * y2 + ... + an * mn * yn
```
其中 `a1, a2, ..., an` 是同余方程组的余数;`m1, m2, ..., mn` 是同余方程组的模数;`y1, y2, ..., yn` 是满足 `mi * yi ≡ 1 (mod mi)` 的数。
可以用 Python 实现中国剩余定理:
```python
def chinese_remainder_theorem(a, m):
"""
中国剩余定理,求解同余方程组
"""
M = 1
for Mi in m:
M *= Mi
x = 0
for ai, Mi in zip(a, m):
bi = M // Mi
x += ai * bi * pow(bi, -1, Mi)
return x % M
```
然后,将同余方程组的余数和模数分别存储在列表 `a` 和 `m` 中,调用 `chinese_remainder_theorem` 函数即可求解。
完整程序如下:
```python
def chinese_remainder_theorem(a, m):
"""
中国剩余定理,求解同余方程组
"""
M = 1
for Mi in m:
M *= Mi
x = 0
for ai, Mi in zip(a, m):
bi = M // Mi
x += ai * bi * pow(bi, -1, Mi)
return x % M
a = [9, 8, 7, 6, 5, 4, 3, 2, 1]
m = [10, 9, 8, 7, 6, 5, 4, 3, 2]
x = chinese_remainder_theorem(a, m)
print(f"广场上士兵的最少人数为 {x}")
```
运行程序,可以得到广场上士兵的最少人数为 2519。
三个差速无人车,一辆车作为领航车,两辆车作为跟随车,基于滑模控制器,先排成三角形,再排成一字型,再排成纵队,python代码
以下是基于滑模控制器实现无人车排成三角形、一字型、纵队的 Python 代码:
```python
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
# 定义控制器参数
k1 = 1
k2 = 1
# 定义无人车初始状态
x1_0 = np.array([0, 0, 0, 0])
x2_0 = np.array([5, 0, 0, 0])
x3_0 = np.array([2.5, 4.3, np.pi/3, 0])
x4_0 = np.array([10, 0, 0, 0])
x5_0 = np.array([7.5, 4.3, np.pi/3, 0])
x6_0 = np.array([5, 8.7, np.pi/3, 0])
# 定义控制输入函数
def u(t, x, x_des):
e = x - x_des
v_des = -k1 * e[:2] - k2 * e[2:]
return np.array([v_des[0] * np.cos(x[2]) + v_des[1] * np.sin(x[2]),
-v_des[0] * np.sin(x[2]) + v_des[1] * np.cos(x[2])])
# 定义运动学函数
def f(t, x, u):
return np.array([x[3] * np.cos(x[2]), x[3] * np.sin(x[2]), u[0], u[1]])
# 定义仿真函数
def simulate(x1_0, x2_0, x3_0, x4_0, x5_0, x6_0, x_des, t_end):
x1 = x1_0
x2 = x2_0
x3 = x3_0
x4 = x4_0
x5 = x5_0
x6 = x6_0
t_eval = np.linspace(0, t_end, 1000)
x1_hist = [x1]
x2_hist = [x2]
x3_hist = [x3]
x4_hist = [x4]
x5_hist = [x5]
x6_hist = [x6]
for t in t_eval:
u1 = u(t, x1, x_des[0])
u2 = u(t, x2, x_des[1])
u3 = u(t, x3, x_des[2])
u4 = u(t, x4, x_des[3])
u5 = u(t, x5, x_des[4])
u6 = u(t, x6, x_des[5])
x1 = solve_ivp(f, (t, t+0.01), x1, args=(u1,)).y[:, -1]
x2 = solve_ivp(f, (t, t+0.01), x2, args=(u2,)).y[:, -1]
x3 = solve_ivp(f, (t, t+0.01), x3, args=(u3,)).y[:, -1]
x4 = solve_ivp(f, (t, t+0.01), x4, args=(u4,)).y[:, -1]
x5 = solve_ivp(f, (t, t+0.01), x5, args=(u5,)).y[:, -1]
x6 = solve_ivp(f, (t, t+0.01), x6, args=(u6,)).y[:, -1]
x1_hist.append(x1)
x2_hist.append(x2)
x3_hist.append(x3)
x4_hist.append(x4)
x5_hist.append(x5)
x6_hist.append(x6)
return np.array(x1_hist), np.array(x2_hist), np.array(x3_hist), np.array(x4_hist), np.array(x5_hist), np.array(x6_hist)
# 排成三角形
x_des = [np.array([0, 0, 0, 0]), np.array([5, 0, 0, 0]), np.array([2.5, 4.3, np.pi/3, 0])]
t_end = 10
x1_hist, x2_hist, x3_hist, _, _, _ = simulate(x1_0, x2_0, x3_0, x4_0, x5_0, x6_0, x_des, t_end)
# 绘制图像
plt.plot(x1_hist[:, 0], x1_hist[:, 1], label='Vehicle 1')
plt.plot(x2_hist[:, 0], x2_hist[:, 1], label='Vehicle 2')
plt.plot(x3_hist[:, 0], x3_hist[:, 1], label='Vehicle 3')
plt.legend()
plt.axis('equal')
plt.show()
# 排成一字型
x_des = [np.array([0, 0, 0, 0]), np.array([5, 0, 0, 0]), np.array([10, 0, 0, 0])]
t_end = 10
x1_hist, x2_hist, _, x4_hist, x5_hist, _ = simulate(x1_0, x2_0, x3_0, x4_0, x5_0, x6_0, x_des, t_end)
# 绘制图像
plt.plot(x1_hist[:, 0], x1_hist[:, 1], label='Vehicle 1')
plt.plot(x2_hist[:, 0], x2_hist[:, 1], label='Vehicle 2')
plt.plot(x4_hist[:, 0], x4_hist[:, 1], label='Vehicle 4')
plt.plot(x5_hist[:, 0], x5_hist[:, 1], label='Vehicle 5')
plt.legend()
plt.axis('equal')
plt.show()
# 排成纵队
x_des = [np.array([0, 0, 0, 0]), np.array([0, 2, np.pi/2, 0]), np.array([0, 4, np.pi/2, 0]),
np.array([0, 6, np.pi/2, 0]), np.array([0, 8, np.pi/2, 0]), np.array([0, 10, np.pi/2, 0])]
t_end = 10
x1_hist, _, _, x4_hist, _, x6_hist = simulate(x1_0, x2_0, x3_0, x4_0, x5_0, x6_0, x_des, t_end)
# 绘制图像
plt.plot(x1_hist[:, 0], x1_hist[:, 1], label='Vehicle 1')
plt.plot(x4_hist[:, 0], x4_hist[:, 1], label='Vehicle 4')
plt.plot(x6_hist[:, 0], x6_hist[:, 1], label='Vehicle 6')
plt.legend()
plt.axis('equal')
plt.show()
```
以上代码中,我们首先定义了控制器参数 `k1` 和 `k2`,以及无人车的初始状态 `x1_0`、`x2_0`、`x3_0`、`x4_0`、`x5_0`、`x6_0`。
然后,我们定义了控制输入函数 `u` 和运动学函数 `f`,并使用 `solve_ivp` 函数进行数值积分。最后,我们通过 `simulate` 函数来模拟三辆无人车排成三角形、一字型和纵队的运动。
在绘制图像时,我们使用 `matplotlib` 库来绘制无人车的运动轨迹。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)