どうやって考えたらそうなったのと言った感じで
how did you come out with that? と表現すると良いでしょう。
何があなたをその答えに至らせたのと言う意味で後者の
what made you come out with that と聞くのも有です。
「一体何を思ってそうなったの?」
"How the hell did you come out with that?"
What thought did you have in your head to give you an answer like that?
この場合、次のような言い方ができますよ。
ーWhat made you come up with an answer like that?
「どうしたらそんな答えを思いつくの?」
to come up with で「考え出す・思いつく」
ーWhat thought did you have in your head to give you an answer like that?
「そんな答えをするなんて、あなたの頭の中はどうなってるの?」
ご参考まで!