怎么用c++画一个爱心?
时间: 2023-08-10 13:05:17 浏览: 147
以下是一个简单的用C++画爱心的例子:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int r = 10; // 爱心半径
for (int y = r; y >= -r; y--) {
for (int x = -r; x <= r; x++) {
double d = (x * x + y * y) - r * r;
if (d <= 0) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
输出结果:
```
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
```
该程序使用双层循环遍历所有坐标点,判断该点是否在爱心的范围内,如果在则打印星号,否则打印空格。可以根据需要调整爱心半径和输出字符。