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])

■□■□■□■□

下面為具體的實(shí)現(xiàn)代碼

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;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,131評(píng)論 0 23
  • 清新高雅伴名儒,居士軒前植幾株。 春暖莫隨香艷色,冬寒定比百花殊。 山青水碧平生愿,酒綠燈紅豈所圖。 有幸身藏先圣...
    七夜迷離閱讀 350評(píng)論 0 0
  • 2017-5-28 王老七 王老七的美好生活 上晚班,有患者需要輸血小板,接到血庫(kù)電話確認(rèn)有血小板之后...
    普通人老七閱讀 608評(píng)論 2 6
  • 最近發(fā)布到百度平臺(tái)的app出現(xiàn)一個(gè)兼容的問(wèn)題,甚是奇怪,網(wǎng)上也沒查到想要的答案。報(bào)錯(cuò)信息:Binary XML f...
    zhongjh閱讀 971評(píng)論 3 2

友情鏈接更多精彩內(nèi)容