📌  相关文章
📜  大于n的最小数字,可以表示为k的不同幂的总和

📅  最后修改于: 2021-06-26 01:52:11             🧑  作者: Mango

给定数字n和值k ,任务是找到最小的m (m> = n),这样m可以表示为k的不同幂的和。
例子:

方法:

  1. 存储n的k进制(基k)表示。然后遍历基k表示形式的每个元素。
  2. 如果此位置的基数k表示是1或0,则如果大于1,则继续。这意味着k的当前幂不止一次出现。
  3. 在那种情况下,该功率被加k(k的位置值)倍,这使其功率增加一倍(((k-1)+1).k x = kk x = k x + 1 )。
  4. 由于必须找到最小的数字,因此在此步骤之后,k的所有低次幂都减小为0,因为加(kb)k x (b =以k为基数表示的那个位置的值)已经使数字大于k的整数。以前的号码。
  5. 最后,将数字转换回十进制形式。

下面是上述方法的实现

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
typedef long long ll;
#define pb push_back
 
// Function to find the smallest number
// greater than or equal to n represented
// as the sum of distinct powers of k
void greaterK(ll n, ll k)
{
 
    // Vector P to store the base k
    // representation of the number
    vector p;
    ll x = n;
    while (x) {
        p.pb(x % k);
        x /= k;
    }
    int idx = 0;
    for (ll i = 0; i < (ll)p.size() - 1; ++i) {
        if (p[i] >= 2) {
 
            // If the representation is >=2, then
            // this power of k has to be added
            // once again and then increase the
            // next power of k and make the
            // current power 0
 
            p[i] = 0;
            p[i + 1]++;
 
            // Reduce all the lower power of
            // k to 0
 
            for (int j = idx; j < i; ++j) {
                p[j] = 0;
            }
            idx = i + 1;
        }
 
        if (p[i] == k) {
            p[i] = 0;
            p[i + 1]++;
        }
    }
    ll j = (ll)p.size() - 1;
 
    // Check if the most significant
    // bit also satisfy the above
    // conditions
 
    if (p[j] >= 2) {
        for (auto& i : p)
            i = 0;
        p.pb(1);
    }
    ll ans = 0;
 
    // Converting back from the
    // k-nary representation to
    // decimal form.
    for (ll i = p.size() - 1; i >= 0; --i) {
        ans = ans * k + p[i];
    }
    cout << ans << endl;
}
 
int main()
{
    ll n = 29, k = 7;
    greaterK(n, k);
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
 
// Function to find the smallest number
// greater than or equal to n represented
// as the sum of distinct powers of k
static void greaterK(int n, int k)
{
 
    // Vector P to store the base k
    // representation of the number
    int []p = new int[String.valueOf(n).length() + 2];
    int index = 0;
    int x = n;
    while (x > 0)
    {
        p[index]=(int) (x % k);
        x /= k;
        index++;
    }
    int idx = 0;
    for (int i = 0; i < p.length - 1; ++i)
    {
        if (p[i] >= 2)
        {
 
            // If the representation is >=2, then
            // this power of k has to be added
            // once again and then increase the
            // next power of k and make the
            // current power 0
            p[i] = 0;
            p[i + 1]++;
 
            // Reduce all the lower power of
            // k to 0
            for (int j = idx; j < i; ++j)
            {
                p[j] = 0;
            }
            idx = i + 1;
        }
 
        if (p[i] == k)
        {
            p[i] = 0;
            p[i + 1]++;
        }
    }
    int j = p.length - 1;
 
    // Check if the most significant
    // bit also satisfy the above
    // conditions
    if (p[j] >= 2)
    {
        p[index] = 1;
        index++;
    }
    int ans = 0;
 
    // Converting back from the
    // k-nary representation to
    // decimal form.
    for (int i = p.length - 1; i >= 0; --i)
    {
        ans = ans * k + p[i];
    }
    System.out.print(ans +"\n");
}
 
// Driver code
public static void main(String[] args)
{
    int n = 29, k = 7;
    greaterK(n, k);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the above approach
 
# Function to find the smallest number
# greater than or equal to n represented
# as the sum of distinct powers of k
def greaterK(n, k):
     
    # Vector P to store the base k
    # representation of the number
    index = 0
    p = [0 for i in range(n + 2)]
    x = n
    while (x > 0):
        p[index] = x % k
        x //= k
        index += 1
 
    idx = 0
    for i in range(0,len(p)-1, 1):
        if (p[i] >= 2):
             
            # If the representation is >=2, then
            # this power of k has to be added
            # once again and then increase the
            # next power of k and make the
            # current power 0
            p[i] = 0
            p[i + 1] += 1
 
            # Reduce all the lower power of
            # k to 0
            for j in range(idx, i, 1):
                p[j] = 0
             
            idx = i + 1
 
        if (p[i] == k):
            p[i] = 0
            p[i + 1] += 1
         
    j = len(p) - 1
 
    # Check if the most significant
    # bit also satisfy the above
    # conditions
    if (p[j] >= 2):
        p[index] = 1
        index += 1
    ans = 0
 
    # Converting back from the
    # k-nary representation to
    # decimal form.
    i = len(p)-1
    while(i>= 0):
        ans = ans * k + p[i]
        i -= 1
    print(ans)
 
if __name__ == '__main__':
    n = 29
    k = 7
    greaterK(n, k)
 
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
 
class GFG{
 
// Function to find the smallest number
// greater than or equal to n represented
// as the sum of distinct powers of k
static void greaterK(int n, int k)
{
 
    // List P to store the base k
    // representation of the number
    int []p = new int[String.Join("",n).Length + 2];
    int index = 0;
    int x = n;
    int j;
    while (x > 0)
    {
        p[index] = (int) (x % k);
        x /= k;
        index++;
    }
    int idx = 0;
    for (int i = 0; i < p.Length - 1; ++i)
    {
        if (p[i] >= 2)
        {
 
            // If the representation is >=2, then
            // this power of k has to be added
            // once again and then increase the
            // next power of k and make the
            // current power 0
            p[i] = 0;
            p[i + 1]++;
 
            // Reduce all the lower power of
            // k to 0
            for (j = idx; j < i; ++j)
            {
                p[j] = 0;
            }
            idx = i + 1;
        }
 
        if (p[i] == k)
        {
            p[i] = 0;
            p[i + 1]++;
        }
    }
    j = p.Length - 1;
 
    // Check if the most significant
    // bit also satisfy the above
    // conditions
    if (p[j] >= 2)
    {
        p[index] = 1;
        index++;
    }
    int ans = 0;
 
    // Converting back from the
    // k-nary representation to
    // decimal form.
    for (int i = p.Length - 1; i >= 0; --i)
    {
        ans = ans * k + p[i];
    }
    Console.Write(ans +"\n");
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 29, k = 7;
    greaterK(n, k);
}
}
 
// This code is contributed by PrinciRaj1992


输出:
49





如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。