I want to stack round rectangles in LaTeX like in this example.

How is this achievable? (I tried the towers of hanoi, but it did not fit well)
I want to stack round rectangles in LaTeX like in this example.

How is this achievable? (I tried the towers of hanoi, but it did not fit well)
You can use the positioning library.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{myrect/.style={fill=gray!50, rounded corners, inner sep=0pt, minimum height=5mm, minimum width=#1}}
\begin{document}
\begin{tikzpicture}[xscale=.6, transform shape, node distance=0pt]
\draw(0,0)--coordinate(A)(8,0);
\nodeabove=of A, myrect=5cm{};
\nodeabove=of B, myrect=7cm{};
\nodeabove=of C, myrect=2cm{};
\nodeabove=of D, myrect=6cm{};
\nodeabove=of E, myrect=3cm{};
\nodeabove=of F, myrect=4cm{};
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}[xscale=.6, transform shape, node distance=0pt]
\draw(0,0)--coordinate(A)(8,0);
\nodeabove=of A, myrect=7cm{};
\nodeabove=of B, myrect=6cm{};
\nodeabove=of C, myrect=5cm{};
\nodeabove=of D, myrect=4cm{};
\nodeabove=of E, myrect=3cm{};
\nodeabove=of F, myrect=2cm{};
\end{tikzpicture}
\end{document}
or if you prefer you could put this into a macro.
Usage is \stack{<bottom center coordinate>}{<stack sequence>}
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{myrect/.style={fill=gray!50, rounded corners, inner sep=0pt, minimum height=5mm, minimum width=#1}}
\newcommand{\stack}[2]{\coordinate(stack1) at (#1);\foreach \n[count=\k, evaluate=\k as \j using int(\k+1)] in {#2}{\nodeabove=of stack\k, myrect=\n cm{};}}
\begin{document}
\begin{tikzpicture}[xscale=.6, transform shape, node distance=0pt]
\draw(0,0)--coordinate(A)(8,0);
\stack{A}{5,7,2,6,3,4}
\draw(9,0)--coordinate(B)(17,0);
\stack{B}{7,6,5,4,3,2}
\end{tikzpicture}
\end{document}
A funny implementation using the l3draw package allowing for alternative and random sorting as well as for using color lists:
\documentclass[border=10pt]{standalone}
\usepackage{l3draw}
\ExplSyntaxOn
\fp_new:N \l_rectstack_base_width_fp
\int_new:N \l_rectstack_rect_count_int
\fp_new:N \l_rectstack_rect_height_fp
\fp_new:N \l_rectstack_rect_sep_fp
\fp_new:N \l_rectstack_rect_min_width_fp
\fp_new:N \l_rectstack_rect_max_width_fp
\dim_new:N \l_rectstack_rect_corner_radius_dim
\clist_new:N \l_rectstack_rect_colors_clist
\clist_new:N \l_rectstack_rect_order_clist
\bool_new:N \l_diststack_random_order_bool
\int_new:N \l__rectstack_rect_color_count_int
\int_new:N \l__rectstack_current_rect_int
\fp_new:N \l__rectstack_current_rect_width_fp
\fp_new:N \l__rectstack_current_rect_ypos_fp
\seq_new:N \l__rectstack_rect_order_seq
\cs_generate_variant:Nn \int_set:Nn { Ne }
\keys_define:nn { rectstack } {
base ~ width .fp_set:N = \l_rectstack_base_width_fp ,
base ~ width .initial:n = { 6cm } ,
rect ~ count .int_set:N = \l_rectstack_rect_count_int ,
rect ~ count .initial:n = { 1 } ,
rect ~ height .fp_set:N = \l_rectstack_rect_height_fp ,
rect ~ height .initial:n = { 0.5cm } ,
rect ~ sep .fp_set:N = \l_rectstack_rect_sep_fp ,
rect ~ sep .initial:n = { 2pt } ,
rect ~ min ~ width .fp_set:N = \l_rectstack_rect_min_width_fp ,
rect ~ min ~ width .initial:n = { 1cm } ,
rect ~ max ~ width .fp_set:N = \l_rectstack_rect_max_width_fp ,
rect ~ max ~ width .initial:n = { 5cm } ,
rect ~ corner ~ radius .dim_set:N = \l_rectstack_rect_corner_radius_dim ,
rect ~ corner ~ radius .initial:n = { 3pt } ,
rect ~ colors .code:n = {
\clist_set:Nn \l_rectstack_rect_colors_clist {#1}
\int_set:Ne \l__rectstack_rect_color_count_int {
\clist_count:N \l_rectstack_rect_colors_clist
}
} ,
rect ~ colors .initial:n = { black!25 },
order .clist_set:N = \l_diststack_rect_order_clist ,
random .bool_set:N = \l_diststack_random_order_bool ,
random .default:n = { true }
}
\cs_generate_variant:Nn \clist_item:Nn { Ne }
\NewDocumentCommand{\drawrectstack}{ O{} }{
\keys_set:nn { rectstack } { #1 }
\bool_if:NT \l_diststack_random_order_bool {
\seq_clear:N \l__rectstack_rect_order_seq
\int_step_inline:nn { \l_rectstack_rect_count_int } {
\seq_put_right:Nn \l__rectstack_rect_order_seq { ##1 }
}
\seq_shuffle:N \l__rectstack_rect_order_seq
\clist_set_from_seq:NN \l_diststack_rect_order_clist \l__rectstack_rect_order_seq
}
\draw_begin:
\draw_path_moveto:n { -0.5 * \l_rectstack_base_width_fp , 0cm }
\draw_path_lineto:n { 0.5 * \l_rectstack_base_width_fp , 0cm }
\draw_path_use_clear:n { stroke }
\draw_path_corner_arc:nn
{ \l_rectstack_rect_corner_radius_dim }
{ \l_rectstack_rect_corner_radius_dim }
\int_step_inline:nn { \l_rectstack_rect_count_int } {
\clist_if_empty:NTF \l_diststack_rect_order_clist {
\int_set:Nn \l__rectstack_current_rect_int { ##1 }
} {
\int_set:Ne \l__rectstack_current_rect_int {
\clist_item:Nn \l_diststack_rect_order_clist { ##1 }
}
}
\fp_set:Nn \l__rectstack_current_rect_width_fp {
\l_rectstack_rect_max_width_fp -
( \l_rectstack_rect_max_width_fp - \l_rectstack_rect_min_width_fp ) /
( \l_rectstack_rect_count_int - 1 ) * ( \l__rectstack_current_rect_int - 1 )
}
\fp_set:Nn \l__rectstack_current_rect_ypos_fp {
( ##1 - 1 ) * \l_rectstack_rect_height_fp + ##1 * \l_rectstack_rect_sep_fp
}
\exp_args:Ne \color_fill:n { \clist_item:Ne \l_rectstack_rect_colors_clist {
\int_mod:nn { ##1 - 1 } { \l__rectstack_rect_color_count_int } + 1 }
}
\draw_path_rectangle_corners:nn
{ -0.5 * \l__rectstack_current_rect_width_fp ,
\l__rectstack_current_rect_ypos_fp }
{ 0.5 * \l__rectstack_current_rect_width_fp ,
\l__rectstack_current_rect_ypos_fp + \l_rectstack_rect_height_fp }
\draw_path_use_clear:n { fill }
}
\draw_end:
}
\ExplSyntaxOff
\begin{document}
\drawrectstack[rect count=7]
\drawrectstack[rect count=5, random, rect colors={cyan}]
\drawrectstack[rect count=6, order={4,1,2,6,5,3}, rect colors={red!20, red, blue, green}]
\end{document}