向日葵代码c++语言
时间: 2023-11-12 15:57:45 浏览: 47
以下是向日葵的C++代码:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
cout << "请输入向日葵的大小(1-10):";
cin >> n;
if (n < 1 || n > 10) {
cout << "输入错误!" << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
```
相关问题
python画图代码向日葵
以下是使用Python的matplotlib库绘制向日葵的代码示例:
```python
import matplotlib.pyplot as plt
import numpy as np
# 设置画布大小和背景色
fig = plt.figure(figsize=(8, 8), facecolor='w')
ax = fig.add_subplot(1, 1, 1)
# 绘制向日葵的花盘
circle = plt.Circle((0, 0), 0.5, color='yellow', ec='black', lw=2)
ax.add_artist(circle)
# 绘制向日葵的花瓣
theta = np.linspace(0, 2 * np.pi, 50)
r = np.sqrt(2)
x = r * np.cos(theta)
y = r * np.sin(theta)
ax.plot(x, y, color='black', lw=2)
# 绘制向日葵的花粉
for i in range(0, 360, 10):
theta = i * np.pi / 180
x = np.cos(theta) * 0.4
y = np.sin(theta) * 0.4
ax.plot(x, y, marker='o', markersize=5, color='black')
# 设置坐标轴和标题
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
ax.set_aspect('equal')
ax.set_title('Sunflower', fontsize=20)
# 显示图形
plt.show()
```
运行代码后,将会得到一张美丽的向日葵图片。
python代码画向日葵
这是一份用Python绘制向日葵的示例代码:
```python
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('skyblue')
# 定义向日葵的函数
def sunflower(x, y, size):
# 绘制花轮
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(0)
turtle.color('brown')
turtle.begin_fill()
turtle.circle(size/2, steps=6)
turtle.end_fill()
# 绘制花瓣
turtle.color('yellow')
for i in range(0, 12):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(i*30)
turtle.begin_fill()
turtle.circle(size/2, steps=3)
turtle.end_fill()
# 绘制中心
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(0)
turtle.color('black')
turtle.begin_fill()
turtle.circle(size/8)
turtle.end_fill()
# 调用函数绘制向日葵
sunflower(0, 0, 200)
# 隐藏画笔
turtle.hideturtle()
# 等待点击关闭窗口
turtle.exitonclick()
```
运行代码后,会弹出一个窗口,显示一个绘制好的向日葵。你也可以根据自己的需求,修改代码中的参数,绘制不同大小和位置的向日葵。