L1-002 打印沙漏 (20 分)
晴神笔记里的代码
//看成上下两个三角形.倒三角和正三角.
然后开始算三角形底部的字符数量.通过n来算…
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n;
char c;
cin >> n >> c;
int bottom = (int)sqrt(2.0 * (n + 1)) - 1;
if(bottom % 2 == 0) bottom --;
int used = (bottom + 1) * (bottom + 1) / 2 - 1;
for (int i = bottom;i >= 1;i -= 2)
{
for(int j = 0;j < (bottom - i) / 2; j ++) cout << ' ';
for(int j = 0;j < i;j ++) cout << c;
puts("");
}
for(int i = 3;i <= bottom;i += 2)
{
for(int j = 0;j < (bottom - i) / 2; j ++) cout << ' ';
for(int j = 0;j < i;j ++) cout << c;
puts("");
}
cout << n - used;
return 0;
}