文章标题

来源:互联网 发布:ubuntu输入法不对 编辑:程序博客网 时间:2024/06/07 02:52

编程题目:https://www.codewars.com/kata/576757b1df89ecf5bd00073b/train/cpp
题目描述:for example, a tower of 3 floors looks like below

[  '  *  ',   ' *** ',   '*****']

思路:调用函数fill_n;

fill_n(beg,n,val)val的值赋给(beg,beg+n)范围内的所有元素,此函数也可用来初始化变量;

源码:

vector<string> towerBuilder(int floor){        vector<string> res;        int pos=2*floor-1;        size_t j=0,k=1;        do{            string temp(pos,' ');            size_t i=pos/2;            fill_n(temp.begin()+i-j,2*k-1,'*');            res.push_back(temp);            k++;            j++;            floor--;            temp.clear();    }while(floor>0);            return res;}
原创粉丝点击