📌  相关文章
📜  5 + 55 + 555 + ..系列的总和,最多n个项

📅  最后修改于: 2021-04-27 19:39:08             🧑  作者: Mango

找出序列中最多n个项的总和:5 + 55 + 555 +…最多n个。
例子 :

Input : 2
Output: 60

Input : 3
Output: 595

方法:可以使用以下公式解决以上问题:

以下是查找给定序列之和的实现:

C++
// C++ program for sum of the
// series 5 + 55 + 555.....n
#include 
using namespace std;
 
// function which return the
// the sum of series
int sumOfSeries(int n)
{
    return 0.6172 *
           (pow(10, n) - 1) -
                    0.55 * n;
     
}
 
// Driver code
int main()
{
    int n = 2;
    cout << sumOfSeries(n);
    return 0;
}


Java
// Java program for sum of the
// series 5 + 55 + 555.....n
 
class GFG
{
 
    // function which return the
    // the sum of series
    static int sumOfSeries(int n)
    {
        return (int) (0.6172 *
                     (Math.pow(10, n) - 1) -
                                0.55 * n);
    }
     
    // Driver code
    public static void main(String []args)
    {
        int n = 2;
        System.out.println(sumOfSeries(n));
    }
}
 
// This code is contributed by UPENDRA BARTWAL.


Python3
# python program for sum of the
# series 5 + 55 + 555.....n
 
def sumOfSeries(n):
    return (int) (0.6172 *
                 (pow(10, n) - 1) -
                        0.55 * n)
 
 
# Driver Code
n = 2
print(sumOfSeries(n))
 
# This code is contributed
# by Upendra Singh Bartwal


C#
// C# program for sum of the
// series 5 + 55 + 555.....n
using System;
 
class GFG
{
 
    // Function which return the
    // the sum of series
    static int sumOfSeries(int n)
    {
        return (int)(0.6172 *
                    (Math.Pow(10, n) - 1) -
                                 0.55 * n);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.Write(sumOfSeries(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

60