国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 克拉玛依市| 榆林市| 绥江县| 庆元县| 乌拉特后旗| 泰顺县| 石家庄市| 修文县| 沅江市| 修文县| 凤翔县| 东乌| 鞍山市| 福安市| 湛江市| 吴川市| 通河县| 临夏市| 广汉市| 抚州市| 德惠市| 宣威市| 嫩江县| 思南县| 红安县| 肇源县| 兴国县| 综艺| 驻马店市| 南京市| 九龙县| 读书| 文安县| 海林市| 奈曼旗| 曲周县| 兴义市| 乌鲁木齐县| 奎屯市| 宁海县| 祁东县|