题目的意思是:
给一个整数,将其分解成从1开始连续的几个数相加,返回那个最大的数。能用数学的先验知识解决的就不要麻烦计算机去做,这样才能最快!n = 5
The coins can form the following rows:
¤
¤ ¤
¤ ¤
Because the 3rd row is incomplete, we return 2.
n = 8
The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
Because the 4th row is incomplete, we return 3.
设最大值是a,a满足(1+a)*a/2<=n,找出满足条件的最大的a。
class Solution {public: int arrangeCoins(int n) {
if(n==0) return 0; int a; a=(int)(sqrt((double)2*n+0.25)-0.5); return a; } };