📜  以分数表示形式打印系列的前 N 项 (0.25, 0.5, 0.75, ...)

📅  最后修改于: 2022-05-13 01:57:05.348000             🧑  作者: Mango

以分数表示形式打印系列的前 N 项 (0.25, 0.5, 0.75, ...)

给定一个整数N ,任务是以分数形式打印系列的前N项,即

上述系列的值为 0.25, 0.5, 0.75, 1, 1.25, ....etc。这是一个算术级数,从 0.25 开始,相差 0.25。
例子:

方法:将系列的前四个术语视为基本术语。分别存储分子元素和分母元素。
考虑第一项1/4 ,第五项是1 + (1 * 4) / 4 ,即1/5
同样,考虑第二项1/2第六项是1 + (1 * 2) / 2 ,即3/2
因此,我们可以认为分母将始终是24没有分母,并且可以从分母计算出该术语的分子。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the required series
void printSeries(int n)
{
    // Numerators for the first four numerators
    // of the series
    int nmtr[4] = { 1, 1, 1, 3 };
 
    // Denominators for the first four denominators
    // of the series
    int dntr[4] = { 0, 4, 2, 4 };
 
    for (int i = 1; i <= n; i++) {
 
        // If location of the term in the series is
        // a multiple of 4 then there will be no denominator
        if (i % 4 == 0)
            cout << nmtr[i % 4] + (i / 4) - 1 << " ";
 
        // Otherwise there will be denominator
        else {
 
            // Printing the numerator and the denominator terms
            cout << nmtr[i % 4] + ((i / 4) * dntr[i % 4])
                 << "/" << dntr[i % 4] << " ";
        }
    }
}
 
// Driver code
int main()
{
    int n = 9;
    printSeries(n);
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
 
// Function to print the required series
public static void printSeries(int n)
{
    // Numerators for the first four numerators
    // of the series
    int[] nmtr = new int[]{ 1, 1, 1, 3 };
 
    // Denominators for the first four denominators
    // of the series
    int[] dntr = new int[]{ 0, 4, 2, 4 };
 
    for (int i = 1; i <= n; i++)
    {
 
        // If location of the term in the series is
        // a multiple of 4 then there will be no denominator
        if (i % 4 == 0)
            System.out.print( nmtr[i % 4] + (i / 4) - 1 + " ");
 
        // Otherwise there will be denominator
        else
        {
 
            // Printing the numerator and the denominator terms
            System.out.print( nmtr[i % 4] + ((i / 4) * dntr[i % 4])
                +"/" + dntr[i % 4] +" ");
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 9;
    printSeries(n);
}
}
 
// This code is contributed
// by 29AjayKumar


Python3
# Python 3 implementation of the approach
 
# Function to print the required series
def printSeries(n):
     
    # Numerators for the first four
    # numerators of the series
    nmtr = [1, 1, 1, 3]
 
    # Denominators for the first four
    # denominators of the series
    dntr = [0, 4, 2, 4]
 
    for i in range(1, n + 1, 1):
         
        # If location of the term in the
        # series is a multiple of 4 then
        # there will be no denominator
        if (i % 4 == 0):
            print(nmtr[i % 4] + int(i / 4) - 1,
                                     end = " ")
 
        # Otherwise there will be denominator
        else:
             
            # Printing the numerator and
            # the denominator terms
            print(nmtr[i % 4] + (int(i / 4) *
                    dntr[i % 4]), end = "")
            print("/", end = "")
            print(dntr[i % 4], end = " ")
 
# Driver code
if __name__ == '__main__':
    n = 9
    printSeries(n)
 
# This code is contributed by
# Shashank_Sharma


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to print the required series
static void printSeries(int n)
{
     
    // Numerators for the first four numerators
    // of the series
    int[] nmtr = { 1, 1, 1, 3 };
 
    // Denominators for the first four denominators
    // of the series
    int[] dntr = { 0, 4, 2, 4 };
 
    for (int i = 1; i <= n; i++)
    {
 
        // If location of the term in the series is
        // a multiple of 4 then there will be no denominator
        if (i % 4 == 0)
            Console.Write((nmtr[i % 4] + (i / 4) - 1) + " ");
 
        // Otherwise there will be denominator
        else
        {
 
            // Printing the numerator and the denominator terms
            Console.Write((nmtr[i % 4] + ((i / 4) * dntr[i % 4])) +
                                        "/" + dntr[i % 4] + " ");
        }
    }
}
 
// Driver code
public static void Main()
{
    int n = 9;
    printSeries(n);
}
}
 
// This code is contributed
// by Akanksha Rai


Javascript


输出:
1/4 1/2 3/4 1 5/4 3/2 7/4 2 9/4