Let the legs be $a,b$, the hypotenuse $c$, the perimeter $P$ and the area $A$. Then $a+b=P-c$ and $ab=2A$, so $(P-c)^2=a^2+b^2+2ab=c^2+4A$. Hence $P^2-2Pc=4A$ and $c=\dfrac{P^2-4A}{2P}$.
With $P=40, A=24$: $c=\dfrac{1600-96}{80}=\dfrac{1504}{80}=\boxed{\dfrac{94}5}$.