编写一个 C++ 程序,使用 for 循环打印菱形数字图案。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter Diamond Number Pattern Row = ";
cin >> rows;
cout << "Diamond Number Pattern\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= rows - i; j++)
{
cout << " ";
}
for(k = 1; k <= i * 2 - 1; k++)
{
cout << k;
}
cout << "\n";
}
for(i = rows - 1; i > 0; i--)
{
for(j = 1; j <= rows - i; j++)
{
cout << " ";
}
for(k = 1; k <= i * 2 - 1; k++)
{
cout << k;
}
cout << "\n";
}
return 0;
}

这个C++示例是打印数字菱形图案的另一种方法。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, l, rows;
cout << "Enter Diamond Number Pattern Row = ";
cin >> rows;
cout << "Diamond Number Pattern\n";
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= rows - i; j++)
{
cout << " ";
}
for(k = i; k >= 1; k--)
{
cout << k;
}
for(l = 2; l <= i; l++)
{
cout << l;
}
cout << "\n";
}
for(i = rows - 1; i > 0; i--)
{
for(j = 1; j <= rows - i; j++)
{
cout << " ";
}
for(k = i; k >= 1; k--)
{
cout << k;
}
for(l = 2; l <= i; l++)
{
cout << l;
}
cout << "\n";
}
return 0;
}
Enter Diamond Number Pattern Row = 9
Diamond Number Pattern
1
212
32123
4321234
543212345
65432123456
7654321234567
876543212345678
98765432123456789
876543212345678
7654321234567
65432123456
543212345
4321234
32123
212
1