6. Given the sequence $\left\{a_{n}\right\}$, where $a_{1}=1, a_{n+1}=\frac{(n+1) a_{n}}{2 n}\left(n \in \mathbf{N}^{*}\right)$, then the sum of the first $n$ terms of the sequence $\left\{a_{n}\right\}$ is $\qquad$.
6. $4-\frac{n+2}{2^{n-1}}$ Hint: From $a_{n+1}=\frac{(n+1) a_{n}}{2 n}$, we know that $\frac{a_{n}}{a_{n-1}}=\frac{n}{2(n-1)}$ $\left(n \in \mathbf{N}^{*}\right.$, and $\left.n \geqslant 2\right)$.
Therefore, when $n \geqslant 2$,
$$
\begin{aligned}
a_{n} & =\frac{a_{n}}{a_{n-1}} \times \frac{a_{n-1}}{a_{n-2}} \times \cdots \times \frac{a_{2}}{a_{1}} \times a_{1} \\
& =\frac{n}{2(n-1)} \times \frac{n-1}{2(n-2)} \times \cdots \times \frac{2}{2 \times 1} \times 1 \\
& =\frac{n}{2^{n-1}} .
\end{aligned}
$$

Also, when $n=1$, $\frac{n}{2^{n-1}}=1=a_{1}$. Therefore, for $n \in \mathbf{N}^{*}$, $a_{n}=\frac{n}{2^{n-1}}$. So,
$$
\begin{array}{c}
S_{n}=1+\frac{2}{2}+\frac{3}{2^{2}}+\cdots+\frac{n}{2^{n-1}}, \\
\frac{1}{2} S_{n}=\frac{1}{2}+\frac{2}{2^{2}}+\frac{3}{2^{3}}+\cdots+\frac{n-1}{2^{n-1}}+\frac{n}{2^{n}} .
\end{array}
$$

Subtracting the two equations, we get
$$
\begin{aligned}
\frac{1}{2} S_{n} & =1+\frac{1}{2}+\frac{1}{2^{2}}+\frac{1}{2^{3}}+\cdots+\frac{1}{2^{n-1}}-\frac{n}{2^{n}} \\
& =\frac{1 \times\left[1-\left(\frac{1}{2}\right)^{n}\right]}{1-\frac{1}{2}}-\frac{n}{2^{n}} \\
& =2-\frac{n+2}{2^{n}} .
\end{aligned}
$$

Therefore, $S_{n}=4-\frac{n+2}{2^{n-1}}$.