粉色的爱心
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
for(float y=1.3;y>=-1.1;y-=0.06){ // 循环控制y坐标的范围,从1.3到-1.1,每次减少0.06
for(float x=-1.1;x<=1.1;x+=0.025) // 循环控制x坐标的范围,从-1.1到1.1,每次增加0.025
{
if(x*x+pow(5.0*y/4.0-sqrt(fabs(x)),2)-1<=0) // 判断当前坐标是否在爱心的范围内
{
cout << "\033[48;2;255;192;203m" << '*' << "\033[0m"; // 设置粉色背景色,并输出*
}
else
cout<<' '; // 输出空格
}
cout<<endl; // 换行
}
system("pause"); // 暂停程序,等待用户按下任意键继续
return 0;
}
效果预览
![图片[1]-使用C++语言实现爱心代码-铭心博客](https://oss.imxbk.com//blog/WordPress/img/wz/%E4%BD%BF%E7%94%A8C%2B%2B%E8%AF%AD%E8%A8%80%E5%AE%9E%E7%8E%B0%E7%88%B1%E5%BF%83%E4%BB%A3%E7%A0%81/1.webp?x-oss-process=image/auto-orient,1/format,webp/watermark,text_d3d3Lnd3cnUuY24,type_ZmFuZ3poZW5naGVpdGk,size_10,g_se,x_10,y_10)
无颜色的爱心
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
for(float y=1.3;y>=-1.1;y-=0.06){ // 循环控制y坐标的范围,从1.3到-1.1,每次减少0.06
for(float x=-1.1;x<=1.1;x+=0.025) // 循环控制x坐标的范围,从-1.1到1.1,每次增加0.025
{
if(x*x+pow(5.0*y/4.0-sqrt(fabs(x)),2)-1<=0) // 判断当前坐标是否在爱心的范围内
{
cout <<'*'; // 输出*
}
else
cout<<' '; // 输出空格
}
cout<<endl; // 换行
}
system("pause"); // 暂停程序,等待用户按下任意键继续
return 0;
}
效果预览
![图片[2]-使用C++语言实现爱心代码-铭心博客](https://oss.imxbk.com//blog/WordPress/img/wz/%E4%BD%BF%E7%94%A8C%2B%2B%E8%AF%AD%E8%A8%80%E5%AE%9E%E7%8E%B0%E7%88%B1%E5%BF%83%E4%BB%A3%E7%A0%81/2.webp?x-oss-process=image/auto-orient,1/format,webp/watermark,text_d3d3Lnd3cnUuY24,type_ZmFuZ3poZW5naGVpdGk,size_10,g_se,x_10,y_10)
© 版权声明
THE END
暂无评论内容