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