题目的意思是:
给一个整数,将其分解成从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;
}
};
本文介绍了一种算法,该算法将一个整数分解为连续的整数之和,并找到这种分解方式中最大的整数。通过数学公式快速解决问题,避免了不必要的计算。
181

被折叠的 条评论
为什么被折叠?



