📌  相关文章
📜  在序列1、1、2、1、2、3、1、2、3、4,…中找到第n个项。

📅  最后修改于: 2021-04-23 06:18:32             🧑  作者: Mango

考虑整数的无限序列:1、1、2、1、2、3、1、2、3、4、1、2、3、4、5…。该序列的构建方式如下:首先写出数字1,然后写出1到2的数字,然后写出1到3的数字,然后写出1到4的数字,依此类推。
在序列的第n个位置上找到数字。
例子 :

Input : n = 3
Output : 2
The 3rd number in the sequence is 2.

Input : 55
Output : 10

第一种方法:想法是首先找到n的块号。为了确定具有第n个数字的块,我们首先从n中减去1(第一个块中的元素数),然后减去2,然后减去3,依此类推,直到得到负n。减数将是块的数量,块中的位置将是我们得到的最后一个非负数。

C++
// CPP program to find the
// value at n-th place in
// the given sequence
#include 
using namespace std;
 
// Returns n-th number in sequence
// 1, 1, 2, 1, 2, 3, 1, 2, 4, ...
int findNumber(int n)
{
    n--;
 
    // One by one subtract counts
    // elements in different blocks
    int i = 1;
    while (n >= 0)
    {
        n -= i;
        ++i;
    }
 
    return (n + i);
}
 
// Driver code
int main()
{
    int n = 3;
    cout << findNumber(n) << endl;
    return 0;
}


Java
// Java program to find the
// value at n-th place in
// the given sequence
 
import java.io.*;
 
class GFG
{
 
    // Returns n-th number in sequence
    // 1, 1, 2, 1, 2, 3, 1, 2, 4, ...
    static int findNumber(int n)
    {
        n--;
 
        // One by one subtract counts
        // elements in different blocks
        int i = 1;
        while (n >= 0)
        {
            n -= i;
            ++i;
        }
 
        return (n + i);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 3;
         
        System.out.println(findNumber(n));
    }
}
 
// This code is contributed by Ajit.


Python3
# Python code to find the value at
# n-th place in the given sequence
 
# Returns n-th number in sequence
# 1, 1, 2, 1, 2, 3, 1, 2, 4, ...
def findNumber( n ):
     
    n -= 1
     
    # One by one subtract counts
    # elements in different blocks
    i = 1
    while n >= 0:
        n -= i
        i += 1
    return (n + i)
 
# Driver code
n = 3
print(findNumber(n))
 
# This code is contributed
# by "Sharad_Bhardwaj".


C#
// C# program to find the
// value at n-th place in
// the given sequence
using System;
 
class GFG
{
 
    // Returns n-th number in sequence
    // 1, 1, 2, 1, 2, 3, 1, 2, 4, ...
    static int findNumber(int n)
    {
        n--;
 
        // One by one subtract counts
        // elements in different blocks
        int i = 1;
        while (n >= 0)
        {
            n -= i;
            ++i;
        }
 
        return (n + i);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 3;
        Console.WriteLine(findNumber(n));
    }
}
 
// This code is contributed by vt_m.


PHP
= 0)
    {
        $n -= $i;
        ++$i;
    }
 
    return ($n + $i);
}
 
// Driver code
$n = 3;
echo findNumber($n);
 
// This code is contributed by anuj_67.
?>


Javascript


C++
// CPP program to find the
// value at n-th place in
// the given sequence
#include 
using namespace std;
 
// Definition of findNumber
// function
int findNumber(int n)
{
    // Finding x from equation
    // n = x(x + 1)/2 + 1
    int x = (int)floor((-1 +
             sqrt(1 + 8 * n - 8)) / 2);
 
    // Base of current block
    int base = (x * (x + 1)) / 2 + 1;
 
    // Value of n-th element
    return n - base + 1;
}
 
// Driver code
int main()
{
    int n = 55;
    cout << findNumber(n) << endl;
    return 0;
}


Java
// Java program to find the
// value at n-th place in
// the given sequence
 
import java.io.*;
 
class GFG
{
 
    // Definition of findNumber function
    static int findNumber(int n)
    {
         
        // Finding x from equation
        // n = x(x + 1)/2 + 1
        int x = (int)Math.floor((-1 +
                Math.sqrt(1 + 8 * n - 8)) / 2);
 
        // Base of current block
        int base = (x * (x + 1)) / 2 + 1;
 
        // Value of n-th element
        return n - base + 1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 55;
         
        System.out.println(findNumber(n));
    }
}
 
// This code is contributed by Ajit.


Python3
# Python program to find
# the value at n-th place
# in the given sequence
import math
 
# Definition of findNumber function
def findNumber( n ):
     
    # Finding x from equation
    # n = x(x + 1)/2 + 1
    x = int(math.floor((-1 + math.sqrt(1
            + 8 * n - 8)) / 2))
 
    # Base of current block
    base = (x * (x + 1)) / 2 + 1
     
    # Value of n-th element
    return n - base + 1
 
# Driver code
n = 55
print(findNumber(n))
 
# This code is contributed
# by "Abhishek Sharma 44"


C#
// C# program to find the
// value at n-th place in
// the given sequence
using System;
 
class GFG
{
 
    // Definition of findNumber function
    static int findNumber(int n)
    {
 
        // Finding x from equation
        // n = x(x + 1)/2 + 1
        int x = (int)Math.Floor((-1 +
        Math.Sqrt(1 + 8 * n - 8)) / 2);
 
        // Base of current block
        int Base = (x * (x + 1)) / 2 + 1;
 
        // Value of n-th element
        return n - Base + 1;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 55;
        Console.WriteLine(findNumber(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

2

时间复杂度:O(√n)
第二种方法:无限序列的答案可以在O(1)中完成。我们可以将序列组织为:1(1)。这意味着该行中第一个是1,2,(2)1,2,3(4)1,2,3,4(7)1,2,3,4,5(11)的位置会有一个新的序列,这很容易找到。 1(1)2(2)4(3)7(4)11括号中的数字是新序列号之间的距离。
为了找到数字的基数,我们需要求解方程n = x(x + 1)/ 2 + 1 [OR x ^ 2 + x + 2 – 2n = 0]。我们需要隔离x(获取最大底值)。
然后,我们使用在相同公式中得到的x,但现在结果将成为该行的基础。我们只需要计算作为输入的数字与作为基数的数字之间的距离。

n — base + 1

C++

// CPP program to find the
// value at n-th place in
// the given sequence
#include 
using namespace std;
 
// Definition of findNumber
// function
int findNumber(int n)
{
    // Finding x from equation
    // n = x(x + 1)/2 + 1
    int x = (int)floor((-1 +
             sqrt(1 + 8 * n - 8)) / 2);
 
    // Base of current block
    int base = (x * (x + 1)) / 2 + 1;
 
    // Value of n-th element
    return n - base + 1;
}
 
// Driver code
int main()
{
    int n = 55;
    cout << findNumber(n) << endl;
    return 0;
}

Java

// Java program to find the
// value at n-th place in
// the given sequence
 
import java.io.*;
 
class GFG
{
 
    // Definition of findNumber function
    static int findNumber(int n)
    {
         
        // Finding x from equation
        // n = x(x + 1)/2 + 1
        int x = (int)Math.floor((-1 +
                Math.sqrt(1 + 8 * n - 8)) / 2);
 
        // Base of current block
        int base = (x * (x + 1)) / 2 + 1;
 
        // Value of n-th element
        return n - base + 1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 55;
         
        System.out.println(findNumber(n));
    }
}
 
// This code is contributed by Ajit.

Python3

# Python program to find
# the value at n-th place
# in the given sequence
import math
 
# Definition of findNumber function
def findNumber( n ):
     
    # Finding x from equation
    # n = x(x + 1)/2 + 1
    x = int(math.floor((-1 + math.sqrt(1
            + 8 * n - 8)) / 2))
 
    # Base of current block
    base = (x * (x + 1)) / 2 + 1
     
    # Value of n-th element
    return n - base + 1
 
# Driver code
n = 55
print(findNumber(n))
 
# This code is contributed
# by "Abhishek Sharma 44"

C#

// C# program to find the
// value at n-th place in
// the given sequence
using System;
 
class GFG
{
 
    // Definition of findNumber function
    static int findNumber(int n)
    {
 
        // Finding x from equation
        // n = x(x + 1)/2 + 1
        int x = (int)Math.Floor((-1 +
        Math.Sqrt(1 + 8 * n - 8)) / 2);
 
        // Base of current block
        int Base = (x * (x + 1)) / 2 + 1;
 
        // Value of n-th element
        return n - Base + 1;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 55;
        Console.WriteLine(findNumber(n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出 :

10