你总共有 枚硬币,你需要将它们摆成一个阶梯形状,第 行就必须正好有 枚硬币。

给定一个数字 n,找出可形成完整阶梯行的总行数。

是一个非负整数,并且在32位有符号整型的范围内。

示例 1:

n = 5

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ 因为第三行不完整,所以返回2.

示例 2:

n = 8

硬币可排列成以下几行:
¤
¤ ¤
¤ ¤ ¤
¤ ¤ 因为第四行不完整,所以返回3.
#include <iostream>

using namespace std;

int arrangeCoins(int n) {
long long nn = n;
return (- + sqrt( + * nn)) / ;
} int main()
{
cout << arrangeCoins(); system("PAUSE");
return ;
}
#include <iostream>

using namespace std;

int arrangeCoins(int n) {
for (long long i = ; i <= n; ++i) {
if (i * (i + ) / > n)
return i - ;
}
} int main()
{
cout << arrangeCoins(); system("PAUSE");
return ;
}

LeetCode 441.排列硬币(C++)的更多相关文章

  1. Java实现 LeetCode 441 排列硬币

    441. 排列硬币 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整 ...

  2. leetcode 441.排列硬币(python)

    1.题目描述 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范 ...

  3. Leetcode之二分法专题-441. 排列硬币(Arranging Coins)

    Leetcode之二分法专题-441. 排列硬币(Arranging Coins) 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形 ...

  4. 【LeetCode】排列硬币

    [问题]你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内. [ ...

  5. [LeetCode] 441. Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  6. [LeetCode] Arranging Coins 排列硬币

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  7. 441 Arranging Coins 排列硬币

    你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内.示例 1:n ...

  8. [Swift]LeetCode441. 排列硬币 | Arranging Coins

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  9. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

随机推荐

  1. springMvc参数传递的方法

    package cn.edu.hj.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; im ...

  2. javascript 文本框值变化触发事件

    javascript 文本框值变化触发事件jo.find(".price").bind('input onpropertychange', function () { me.cal ...

  3. day05.1-文件归档与压缩

    >:覆盖式修改文件内容.如: a). cat   /etc/passwd   >   new_pass.txt(将/etc/passwd中的内容覆盖式复制到new_pass.txt中,若n ...

  4. 502. IPO

    Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...

  5. loj #2055. 「TJOI / HEOI2016」排序

    #2055. 「TJOI / HEOI2016」排序   题目描述 在 2016 年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他. 这个 ...

  6. 51nod1040最大公约数之和(欧拉函数)

    题面 传送门 题解 这种题目就是推倒推倒 \[\sum_{i=1}^n \gcd(i,n)=\sum_{i|n}i\sum_{j=1}^n[\gcd(j,n)=i]\] \[\sum_{i=1}^n ...

  7. C#中==与equal的区别

    值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈中仅仅是存储引用类型变量的地址,而其本身则存储在堆中. ==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地 ...

  8. P2381 圆圆舞蹈

    题意:一个圆,上面有n头牛(卧槽) 给出相邻两头牛顺时针的距离 问两只最远的牛的距离(min(顺时针距离,逆时针距离)) 最远距离一定$\le$距离和/2 先求个前缀和 那么问题转化为:找到 $s_j ...

  9. CF912E Prime Gift 数学

    Opposite to Grisha's nice behavior, Oleg, though he has an entire year at his disposal, didn't manag ...

  10. list与str互转

    import stringstr = 'abcde' list = list(str)list['a', 'b', 'c', 'd', 'e']str'abcde'str_convert = ''.j ...