5. Let the equation
$$
1+x-\frac{x^{2}}{2}+\frac{x^{3}}{3}-\frac{x^{4}}{4}+\cdots-\frac{x^{2018}}{2018}=0
$$

have all its real roots within the interval $[a, b](a, b \in \mathbf{Z}$, $a<b)$. Then the minimum value of $b-a$ is ( ).
(A) 1
(B) 2
(C) 3
(D) 4
5. C.

Let $f(x)=1+\sum_{i=1}^{2018}(-1)^{i-1} \frac{x^{i}}{i}$.
Then $f^{\prime}(x)=\sum_{i=0}^{2017}(-1)^{i} x^{i}$.
When $x<1$,
$$
f^{\prime}(x)=\sum_{i=0}^{1008} x^{2 i}(1-x)>0,
$$
hence $f(x)$ is an increasing function;
When $x=1$, $f^{\prime}(x)=0$;
When $x>1$,
$$
f^{\prime}(x)=\sum_{i=0}^{1008} x^{2 i}(1-x)<0, \\
f(1)=1+\sum_{i=1}^{1009}\left(\frac{1}{2 i-1}-\frac{1}{2 i}\right)>0, \\
f(2)=1+\sum_{i=1}^{1009}\left(\frac{2^{2 i-1}}{2 i-1}-\frac{2^{2 i}}{2 i}\right) \\
<1+0+\left(\frac{8}{3}-4\right)<0 .
\end{array}
$$

Thus, $f(x)$ has only two zeros, one in $(-1,0)$ and the other in $(1,2)$.
Therefore, $a \leqslant-1, b \geqslant 2 \Rightarrow b-a \geqslant 3$.
When $a=-1, b=2$, the equality holds. Hence, the minimum value of $b-a$ is 3.