1. 空心正方形
代码:
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
if ((i == 0) || (i == n-1) )
{
for (int j = 0; j < n; j++)
{
cout << "*";
}
}
else
{
for (int j = 0; j < n; j++)
{
if ((j == 0) || (j == n - 1))
{
cout << "*";
}
else
{
cout << " ";
}
}
}
cout << endl;
}
return 0;
}
结果:
2. 数字正方形
代码:
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n*n; i++)
{
printf("%4d", i);
if (i%n == 0)
{
cout << endl;
}
}
return 0;
}
结果: