Compound interest with varying compounding frequency
This problem is a good example of problems that can be solved by dynamic programming.

Given original principal $n$, the subproblems are, what is $p(t)$ for $0\le t\le r$, the maximum final principal that can be yielded if aggregate rate is $t$ . We have the following recurrence relation, where $//$ means the integer division, for example, $13 \operatorname{//} 5 = 2$. 

$$p(t) = \max( p(0) + p(0)* t \operatorname{//} 100,\ p(1)+p(1) * (t-1) \operatorname{//} 100,\ p(2)+ p(2) * (t-2) \operatorname{//}100,\ \cdots,\ p(t-1)+ p(t-1) * 1 \operatorname{//} 100)
$$

From $p(0)=p$ you can compute $p(1)$. From $p(0)$ and $p(1)$, you can compute $p(2)$. And so on. Finally you can will get $p(r)$, which is what you want.

I will left you figure out the actual implementation. By the way, the code in question, fails when the original principal is 23 and the aggregate rate r is 9.