9 solutions
-
-1
基础代码无需复杂操作:
#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> using namespace std; typedef long long int lint; const int N=1e5+1,inf=0x3F3F3F3F; int n; int main(){ //freopen("1.in","r",stdin); cin>>n; //步骤一:构建一个倒三角 for(int i=n;i>=1;i-=2){ for(int j=1;j<=(n-i)/2;j++) cout<<' '; //需要先填充空格 因为空格 + 当前层的字符数量 = 我们的最外层字符数量 // 所以可以直接求空格数量 由于我们只需要填充左侧的空格,所以还需要除以2 for(int j=1;j<=i;j++ ) cout<<'*';//输出字符 cout<<endl; } //步骤二:构建无顶部的正三角型,方法不变 for(int i=3;i<=n;i+=2){ for(int j=1;j<=(n-i)/2;j++) cout<<' '; for(int j=1;j<=i;j++) cout<<'*'; cout<<endl; } //这里就完成了沙漏的输出,基础代码无需复杂操作 return 0; }
Information
- ID
- 67
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 1
- Tags
- (None)
- # Submissions
- 138
- Accepted
- 65
- Uploaded By