I have no problem with armed pilots if for no other reason than if the pilot wants to carry, he or she should be able to. But I also don't see how armed pilots are going to stop a terrorist from taking over the airplane if the cabin door is as secure as it is supposed to be. The goal is to keep the pilot in the cockpit so in the event of a terror threat, he or she can safely land the plane ASAP. Once an armed pilot leaves the cockpit, there is always a chance that the bad guys are better shots than the pilot or are otherwise able to overwhelm the pilot thereby taking control of the plane and using it as a weapon. Again, if a pilot wants to carry, then by all means do so. But once the plane is in the air, the only reason the pilot should ever have to use the weapon is if somehow the enemy gains access to the flight deck.
You are aware that terrorists may be infiltrating flight crews, so the pilot with the gun may be al Qaeda?
See the little problem with your "solution"?
They better do an extremely thorough vetting of the marshalls as it is.