好的,所以我必须创建这个菱形,它应该是菱形,但右上半部分缺失,左下半部分缺失。我有两个形状,但它们没有以菱形形式显示,而是打印出一个在另一个之上。
我假设某处。我必须打印出相同数量的空间才能将整个底部形状向右移动?
#include <iostream>
using namespace std;
int main()
{
int row, col, siz;
cout << "Enter the size of the diamond: " << endl;
cin >> siz;
cout << endl << endl;
for(row = 1; row <= siz; row++){
for(col = 1; col <= siz; col ++){
if(col <= siz - row){
cout << " ";
}
else{
cout << "@";
}
}
cout << "\n";
}
for(row = 1; row <= siz; row++){
for(col = row; col <= siz; col++){
cout << "@";
}
cout << "\n";
}
return 0;
}
最佳答案
这是我对你问题的解决方案:
#include <iostream>
using namespace std;
int main()
{
int row, col, siz;
cout << "Enter the size of the diamond: " << endl;
cin >> siz;
cout << endl << endl;
// drawing top left side of diamon
for(row = 1; row <= siz; row++){
for(col = 1; col <= siz; col ++){
if(col <= siz - row){
cout << " ";
}
else{
cout << "@";
}
}
cout << "\n";
}
// drawing bottom right side of diamon
for(row = 1; row <= siz; row++){
for(col = 1; col <= siz*2-row; col++){
if(col<siz){
cout << " ";
}
else{
cout << "@";
}
}
cout << "\n";
}
return 0;
}
解释:
下半场的第一列必须先绘制“siz”时间“”,然后再绘制“siz”时间“@”。所以它必须循环 siz*2;从那时起你总是有'siz'时间''然后每次都少一个'@'所以这就是为什么循环应该看起来像for(col = 1; col <= siz*2-row; col++)
关于c++ - 钻石形状打印不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967776/