finally solved by my own method, not very good, read other's elegant code, learn a lot