码迷,mamicode.com
首页 > 编程语言 > 详细

(C++)输入符号及符号个数打印沙漏

时间:2019-03-01 18:20:57      阅读:648      评论:0      收藏:0      [点我收藏+]

标签:img   cout   判断   detail   +=   ios   src   std   pre   

技术图片

 

技术图片

 

思路:想将所有沙漏所需符号数遍历一遍,然后根据输入的数判断需要输出多少多少层的沙漏,然后分两部分输出沙漏。
 
 
 1 #include<iostream>
 2 
 3  
 4 
 5 using namespace std;
 6 
 7  
 8 
 9 int main(void)
10 
11 {
12 
13     int num;
14 
15     char c;
16 
17     int a[23]={0};
18 
19     a[0]=1;
20 
21     int sum=1,e;
22 
23     cin>>num>>c;
24 
25     for(int i=1;i<=22;i++)
26 
27     {
28 
29         sum+=(2*i+1)*2;
30 
31         a[i]=sum;
32 
33     }
34 
35     for(int i=22;i>=0;i--)
36 
37     {
38 
39         if(num>=a[i])
40 
41         {
42 
43             e=i;
44 
45             break;
46 
47         }
48 
49     }
50 
51     if(e==0)
52 
53     {
54 
55         cout<<c<<endl<<"0";
56 
57         return 0;
58 
59     }
60 
61     for(int i=e;i>=0;i--)
62 
63     {
64 
65         for(int jj=e-i;jj>0;jj--)
66 
67             cout<<" ";
68 
69         for(int j=0;j<2*i+1;j++)
70 
71             cout<<c;
72 
73         cout<<endl;
74 
75     }
76 
77     for(int i=e;i>0;i--)
78 
79     {
80 
81         for(int jj=i-1;jj>0;jj--)
82 
83             cout<<" ";
84 
85         for(int j=0;j<(e-i+1)*2+1;j++)
86 
87             cout<<c;
88 
89         cout<<endl;
90 
91     }
92 
93     cout<<num-a[e];
94 
95     return 0;
96 
97 }

 

 
--------------------------------------------------------------------------------------------------------------------------------------------------------------
作者:Vansnc
来源:CSDN
原文:https://blog.csdn.net/vansnc/article/details/81458526
版权声明:本文为博主原创文章,转载请附上博文链接!

(C++)输入符号及符号个数打印沙漏

标签:img   cout   判断   detail   +=   ios   src   std   pre   

原文地址:https://www.cnblogs.com/Yethon/p/10457900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!