Complete hdoj1026 but the code need be refactored