用C++写一个爱心
时间: 2023-11-17 21:08:15 浏览: 39
以下是用C++代码实现爱心的方法:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double PI = acos(-1.0);
const int R = 10; // 爱心半径
for (int y = R; y >= -R; --y) {
for (int x = -R; x <= R; ++x) {
double d = sqrt(x * x + y * y);
if (d <= R * sin(PI / 4 - atan2(y, x))) {
cout << "❤️ "; // ❤️ 是 Unicode 编码中的爱心符号
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
运行结果:
```
❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️ ❤️
❤️
```