123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- /* Write a function to create a Christmas tree based on height h.
- * tree(0) ➞ []
- * tree(1) ➞
- "#"
- tree(2) ➞
- " # ",
- "###"
- tree(5) ➞
- " # ",
- " ### ",
- " ##### ",
- " ####### ",
- "#########"
- // 0000 5 0000
- // 000 555 000
- // 00 55555 00
- // 0 5555555 0
- // 555555555
- // the "whorls" [each level of the tree] are odd in length.
- // the integer passed to the function will create
- // "h" amount of strings.
- // the length of each whorl is "h" +"h-1"
- // airSpace ("air") around the branches is "air"
- std::vector<std::string> treeX(int h) {
- std::vector<std::string> whorl;
- int maxWhorl = h + (h-1);
- for(;h>0;h--){
- c = "#";
- // create the base of the tree & maxWhorl
- }
- }
- */
- std::vector<std::string> tree(int h) {
- std::vector<std::string> r;
- // set the base whorl (abs protects against 0 height tree)
- std::string base = std::string(abs(h+h-1),'#');
- for(int i=0;i<h;i++){
- // prepend previous whorl to tree
- r.insert(r.begin(),base);
- // reduce size of whorl for next loop
- base[base.size()-i-1] = ' ';
- base[i] = ' ';
- }
- return r;
- }
|