题目 727. 菱形
输入一个奇数 n ,输出一个由 * 构成的 n 阶实心菱形。
输入格式
一个奇数 n 。
输出格式
输出一个由 * 构成的 n 阶实心菱形。
具体格式参照输出样例。
数据范围
1≤n≤99
输入样例:
5
输出样例:
*
***
*****
***
*
找规律1:将菱形分为上下两段找规律,具体从行号,首项,尾项之间找规律
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n / 2 + 1; i ++ )
{
int k = n / 2 - i;
int j = 2 * i + 1;
while (k --) cout << ' ';
while (j --) cout << '*';
cout << endl;
}
for (int i = 0; i < n / 2 + 1 ; i ++ )
{
int k = i;
int j = n - 2 * i;
while (k --) cout << ' ';
while (j --) cout << '*';
cout << endl;
}
return 0;
}
240222
欧几里得距离 sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2))
曼哈顿距离 | x1 - x2 | + | y1 - y2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i ++ )
{
for (int j = 0 ; j < n; j ++ )
{
if (abs(n / 2 - i) + abs(n /2 - j) <= n / 2) cout << '*';
else cout << " ";
}
cout << endl;
}
return 0;
}