The king was looking to hire a new scribe. Three bright young lads applied for the job. The king, who was widely known for being very just, made them undergo a test. He took three red and three black hats, and told them that the one who will guess the color of his own hat, will win the honorable title of being the king's scribe. The applicants were blindfolded, a red hat put on the head of each of them, and the three black hats were hidden away.
After the blindfolds were removed, the three applicants stood there for a while in silence, thinking. Each of them could see the hats of the other two, but none of them could see their own hat.
After some time, one of them said that he has a red hat on his head, correctly guessing the solution to the test.
What was the logic behind it?