Arranging Coins

来源:互联网 发布:视频翻译字幕软件 编辑:程序博客网 时间:2024/06/06 14:24

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个硬币,第k级台阶放k个硬币,比如第1级放1个硬币,第2级放2个硬币,请问这n个硬币可以按这种方式摆上几级台阶(纯白话翻译)

解法一:

思路:

数学问题,等差数列求和。。(1+k)k/2 = n….已知n求解k,用求根公式。。。

Code:

public class Solution {    public int arrangeCoins(int n) {        return (int)((-1 + Math.sqrt(1 + 8*(long)n))/2);    }}
0 0
原创粉丝点击