📌  相关文章
📜  第n个数字中的位数由给定的四位数字组成

📅  最后修改于: 2021-05-04 09:14:52             🧑  作者: Mango

在以6、1、4和9为升序的唯一数字中找到第n个数字中的位数。
通过仅使用6、1、4和9作为升序构造的前几个数字将是:1、6、4,
9、11、14、16、19、41、44、46、49、61、64、66、69、91、94、96、99、111、114、116、119等。

例子:

Input : 6
Output : 2
6th digit of the series is 14 which has 2 digits.

Input : 21
Output : 3
21st digit of the series is 111 which has 3 digits.

简单方法:这是一种蛮力方法。
1.将数字初始化为1,将计数器初始化为0。
2.检查初始化的数字是否只有6、1、4或9。
3.如果只有提到的数字,则将计数器增加1。
4.增加数量并重复上述步骤,直到计数器小于n。
注意: n的值可能很大,因此这种方法由于时间效率不高而无法使用。

高效的方法:您可以计算O(1)时间中的k位数字的数量,并且它们始终为4的幂,例如,该系列中1位数字的数量为4,该系列中2位数字的数量将是16,依此类推。

  1. 计算所有后续的k位数字,并将其加到总和中。
  2. 当sum大于或等于n时,中断循环。
  3. 维护一个计数器以跟踪数字位数。
  4. 循环中断时计数器的值将指示答案。

下面是上述方法的实现:

C++
// C++ program to count number of digits
// in n-th number made of given four digits.
#include 
using namespace std;
 
// Efficient function to calculate number
// of digits in the nth number constructed
// by using 6, 1, 4 and 9 as digits in the
// ascending order.
int number_of_digits(int n)
{
    int i, res, sum = 0;
 
    // Number of digits increase after
    // every i-th number where i increases in
    // powers of 4.
    for (i = 4, res = 1;; i *= 4, res++) {
        sum += i;
        if (sum >= n)
            break;       
    }
    return res;
}
 
// Driver code
int main()
{
    int n = 21;
    cout << number_of_digits(n) << endl;
    return 0;
}
//Thic code is contributed by Mayank Tyagi


Java
// Java program to count
// number of digits in
// n-th number made of
// given four digits.
import java.io.*;
 
class GFG {
 
    // Efficient function to
    // calculate number of digits
    // in the nth number constructed
    // by using 6, 1, 4 and 9 as
    // digits in the ascending order.
    static int number_of_digits(int n)
    {
        int i;
        int res;
        int sum = 0;
 
        // Number of digits increase
        // after every i-th number
        // where i increases in powers of 4.
        for (i = 4, res = 1;; i *= 4, res++) {
            sum += i;
            if (sum >= n)
                break;
        }
        return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 21;
        System.out.println(number_of_digits(n));
    }
}
 
// This code is contributed
// by akt_mit


Python3
# Python3 program to count number of
# digits in n-th number made of given
# four digits.
 
# Efficient function to calculate number
# of digits in the nth number constructed
# by using 6, 1, 4 and 9 as digits in the
# ascending order.
 
 
def number_of_digits(n):
 
    i = 4
    res = 1
    sum = 0
 
    # Number of digits increase after
    # every i-th number where i increases
    # in powers of 4.
    while(True):
        i *= 4
        res += 1
        sum += i
        if(sum >= n):
            break
    return res
 
 
# Driver Code
n = 21
print(number_of_digits(n))
 
# This code is contributed by mits


C#
// C#  program to count
// number of digits in
// n-th number made of
// given four digits.
 
using System;
 
public class GFG {
 
    // Efficient function to
    // calculate number of digits
    // in the nth number constructed
    // by using 6, 1, 4 and 9 as
    // digits in the ascending order.
    static int number_of_digits(int n)
    {
        int i;
        int res;
        int sum = 0;
 
        // Number of digits increase
        // after every i-th number
        // where i increases in powers of 4.
        for (i = 4, res = 1;; i *= 4, res++) {
            sum += i;
            if (sum >= n)
                break;
        }
        return res;
    }
 
    // Driver Code
 
    static public void Main()
    {
 
        int n = 21;
        Console.WriteLine(number_of_digits(n));
    }
}


PHP
= $n)
            break;
    }
    return $res;
}
 
// Driver Code
$n = 21;
echo number_of_digits($n),"\n";
     
// This code is contributed by ajit
?>


Javascript


输出
3

注意:由于n可能真的很大,所以我们使用了boost库,要了解有关boost库的更多信息,请阅读本文:带有boost库的高级C++