首页 > 学院 > 开发设计 > 正文

LeetCode-441. Arranging Coins

2019-11-08 01:09:10
字体:
来源:转载
供稿:网友

问题:https://leetcode.com/PRoblems/arranging-coins/?tab=Description 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. 你有n枚硬币,想要用来组成一个完整的楼梯,第m层有m个硬币。给出n,返回可以组成的完整楼梯的层数。n是一个非负数,且满足32位int的范围。 分析:等差数列排放的形式。对于第m层,排满的要求是剩下的硬币数量大于等于m。 C++代码:

class Solution {public: int arrangeCoins(int n) { int num=0; int i=1; while(n>=i){ num++; n=n-i; i=i+1; } return num; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表