我想用一个内心的单词来画一个心形,作为明天给 friend 的惊喜,但是我不知道该如何将这些单词放到心里。我只能画心形
代码画心
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x,y;
double size=10;
for (x=0;x<size;x++)
{
for (y=0;y<=4*size;y++)
{
double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
if (dist1 < size + 0.5 || dist2 < size + 0.5 )
cout<<"V";
else
cout<<" ";
}
cout<<endl;
}
for ( x=1;x<2*size;x++)
{
for(y=0;y<x;y++)
cout<<" ";
for (y=0; y<4*size + 1 - 2*x; y++)
cout<<"V";
cout<<endl;
}
system("PAUSE");
}
我需要帮助将单词放入心脏形状
最佳答案
与其他答案几乎相同,但是我已经开始,所以我认为我也可以完成。另外,您可以指定打印“V”字形的行。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x, y, size=10;
string message(" hello there ");
int print_line = 4;
if (message.length() % 2 != 0) message += " ";
for (x=0;x<size;x++)
{
for (y=0;y<=4*size;y++)
{
double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
cout << "V";
}
else cout << " ";
}
cout<<"\n";
}
for (x=1;x<2*size;x++)
{
for(y=0;y<x;y++) cout << " ";
for (y=0; y<4*size + 1 - 2*x; y++)
{
if (x >= print_line - 1 && x <= print_line + 1) {
int idx = y - (4*size - 2*x - message.length()) / 2;
if (idx < message.length() && idx >= 0) {
if (x == print_line) cout<<message[idx];
else cout << " ";
}
else cout << "V";
}
else cout << "V";
}
cout<<endl;
}
}