国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

LEETCODE-- Arranging Coins

2019-11-08 01:49:24
字體:
供稿:網(wǎng)友

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of a 32-bit signed integer. Example 1: n = 5

The coins can form the following rows: ¤ ¤ ¤ ¤ ¤

Because the 3rd row is incomplete, we return 2.

Example 2: n = 8

The coins can form the following rows: ¤ ¤ ¤ ¤ ¤ ¤ ¤ ¤

Because the 4th row is incomplete, we return 3. 方法一:

class Solution {public: int arrangeCoins(int n) { int i; for (i=1; n>=0; ++i){ n -= i; } return i - 2; }};

方法二: 利用等差數(shù)列求和公式,并且注意要先將n轉(zhuǎn)化為double類型

class Solution {public: int arrangeCoins(int n) { return (-1 + sqrt(8 * (double)n + 1) )/ 2; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 扶余县| 古丈县| 济南市| 凤阳县| 南溪县| 肃南| 榆社县| 西藏| 高尔夫| 丹巴县| 柳江县| 龙海市| 元江| 铜山县| 乐山市| 五河县| 太谷县| 阳西县| 九龙坡区| 武鸣县| 隆德县| 临江市| 衡阳县| 武平县| 溧阳市| 庆安县| 楚雄市| 西充县| 临颍县| 高安市| 东乌珠穆沁旗| 昔阳县| 麟游县| 冷水江市| 通榆县| 惠东县| 红桥区| 洛宁县| 广河县| 龙江县| 平邑县|