Codewars #■□ Pattern □■ : Wave

Task:

Complete the pattern, using the special character

■ □
In this kata, we draw some histogram of the sound performance of ups and downs.

Rules:

parameter waves The value of sound waves, an array of number, all number in array >=0.
return a string, ■ represents the sound waves, and □ represents the blank part, draw the histogram from bottom to top.

Example:

draw([1,2,3,4])

□□□■
□□■■
□■■■
■■■■

draw([1,2,3,3,2,1])

□□■■□□
□■■■■□
■■■■■■

draw([1,2,3,3,2,1,1,2,3,4,5,6,7])

□□□□□□□□□□□□■
□□□□□□□□□□□■■
□□□□□□□□□□■■■
□□□□□□□□□■■■■
□□■■□□□□■■■■■
□■■■■□□■■■■■■
■■■■■■■■■■■■■


draw([5,3,1,2,4,6,5,4,2,3,5,2,1])

□□□□□■□□□□□□□
■□□□□■■□□□■□□
■□□□■■■■□□■□□
■■□□■■■■□■■□□
■■□■■■■■■■■■□
■■■■■■■■■■■■■

draw([1,0,1,0,1,0,1,0])

■□■□■□■□

下面为具体的实现代码

std::string draw(std::vector<int> waves)
{
    std::string s;
    //求waves里的最大值
    auto max_iter = std::max_element(waves.begin(), waves.end());
    int max=*max_iter;
    for(int i=max;i>0;--i)
    {
      for(int j:waves)
      {
        if(j>=i)
        {
          s.append ("■");
        }else{
          s.append ("□");
        }
      }
      if(i>1)
        s.append("\n");
    }
    return s;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 11,204评论 0 23
  • 清新高雅伴名儒,居士轩前植几株。 春暖莫随香艳色,冬寒定比百花殊。 山青水碧平生愿,酒绿灯红岂所图。 有幸身藏先圣...
    七夜迷离阅读 350评论 0 0
  • 2017-5-28 王老七 王老七的美好生活 上晚班,有患者需要输血小板,接到血库电话确认有血小板之后...
    普通人老七阅读 610评论 2 6
  • 最近发布到百度平台的app出现一个兼容的问题,甚是奇怪,网上也没查到想要的答案。报错信息:Binary XML f...
    zhongjh阅读 976评论 3 2

友情链接更多精彩内容