Can any one slove this question? It is really hard.
http://img168.imagehshack.us/img168/7080/task3vn7.png

I don't know how to write it in c++ but i have write up the programing logic that should work for this question.