📌  相关文章
📜  找出最接近 N 且最多有一位非零数字的较大数字

📅  最后修改于: 2021-10-26 05:28:09             🧑  作者: Mango

给定一个整数N ,任务是找到最接近N且大于N且最多包含一个非零数字的数字

例子:

方法:该问题可以基于以下观察来解决。

请按照以下步骤解决问题:

  1. 初始化一个变量,比如ctr来存储N 中的数字计数。
  2. 计算power(10, ctr – 1 ) 的值
  3. 打印上述公式的值作为所需答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to calculate
// X ^ n in log(n)
int power(int X, int n) {
     
    // Stores the value
    // of X^n
    int res = 1;
    while(n) {
 
        // If N is odd
        if(n & 1)
        res = res * X;
         
        X = X * X;
        n = n >> 1;
    }
    return res;
     
}
 
// Function to find the
// closest number > N having
// at most 1 non-zero digit
int closestgtNum(int N) {
     
    // Stores the count
    // of digits in N
    int n = log10(N) + 1;
     
    // Stores the power
    // of 10^(n-1)
    int P = power(10, n - 1);
     
    // Stores the
    // last (n - 1) digits
    int Y = N % P;
     
    // Store the answer
    int res = N + (P - Y);
     
    return res;
}
 
// Driver Code
int main()
{
    int N = 120;
    cout<


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
  
// Function to calculate
// X ^ n in log(n)
static int power(int X, int n)
{
     
    // Stores the value
    // of X^n
    int res = 1;
     
    while(n != 0)
    {
         
        // If N is odd
        if ((n & 1) != 0)
            res = res * X;
          
        X = X * X;
        n = n >> 1;
    }
    return res;
}
  
// Function to find the
// closest number > N having
// at most 1 non-zero digit
static int closestgtNum(int N)
{
     
    // Stores the count
    // of digits in N
    int n = (int) Math.log10(N) + 1;
      
    // Stores the power
    // of 10^(n-1)
    int P = power(10, n - 1);
      
    // Stores the
    // last (n - 1) digits
    int Y = N % P;
      
    // Store the answer
    int res = N + (P - Y);
      
    return res;
}
  
// Driver Code
public static void main (String[] args)
{
    int N = 120;
  
    // Function call
    System.out.print(closestgtNum(N));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
import math
 
# Function to calculate
# X ^ n in log(n)
def power(X, n):
      
    # Stores the value
    # of X^n
    res = 1
     
    while (n != 0):
  
        # If N is odd
        if (n & 1 != 0):
            res = res * X
          
        X = X * X
        n = n >> 1
     
    return res
      
# Function to find the
# closest number > N having
# at most 1 non-zero digit
def closestgtNum(N):
      
    # Stores the count
    # of digits in N
    n = int(math.log10(N) + 1)
      
    # Stores the power
    # of 10^(n-1)
    P = power(10, n - 1)
      
    # Stores the
    # last (n - 1) digits
    Y = N % P
      
    # Store the answer
    res = N + (P - Y)
      
    return res
 
# Driver Code
N = 120
 
print(closestgtNum(N))
 
# This code is contributed by code_hunt


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
  
// Function to calculate
// X ^ n in log(n)
static int power(int X, int n)
{
     
    // Stores the value
    // of X^n
    int res = 1;
     
    while(n != 0)
    {
         
        // If N is odd
        if ((n & 1) != 0)
            res = res * X;
          
        X = X * X;
        n = n >> 1;
    }
    return res;
}
  
// Function to find the
// closest number > N having
// at most 1 non-zero digit
static int closestgtNum(int N)
{
     
    // Stores the count
    // of digits in N
    int n = (int) Math.Log10(N) + 1;
      
    // Stores the power
    // of 10^(n-1)
    int P = power(10, n - 1);
      
    // Stores the
    // last (n - 1) digits
    int Y = N % P;
      
    // Store the answer
    int res = N + (P - Y);
      
    return res;
}
  
// Driver Code
public static void Main ()
{
    int N = 120;
  
    // Function call
    Console.Write(closestgtNum(N));
}
}
 
// This code is contributed by code_hunt


Javascript


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to get closest greater
// number with at most non zero digit
string closestgtNum(string str)
{
    // Stores the closest greater number
    // with at most one non-zero digit
    string res = "";
     
    // Stores length of str
    int n = str.length();
     
     
    if(str[0] < '9') {
        res.push_back(str[0] + 1);
    }
    else{
         
        // Append 10 to the end
        // of resultant string
        res.push_back('1');
        res.push_back('0');
    }
     
    // Append n-1 times '0' to the end
    // of resultant string
    for(int i = 0; i < n - 1; i++)
    {
        res.push_back('0');
    }
    return res;
     
     
}
 
// Driver Code
int main()
{
    string str = "120";
    cout<


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to get closest greater
// number with at most non zero digit
static String closestgtNum(String str)
{
     
    // Stores the closest greater number
    // with at most one non-zero digit
    String res = "";
     
    // Stores length of str
    int n = str.length();
     
    if (str.charAt(0) < '9')
    {
        res += (char)(str.charAt(0) + 1);
    }
    else
    {
         
        // Append 10 to the end
        // of resultant String
        res += (char)('1');
        res += (char)('0');
    }
     
    // Append n-1 times '0' to the end
    // of resultant String
    for(int i = 0; i < n - 1; i++)
    {
        res += (char)('0');
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "120";
     
    System.out.print(closestgtNum(str));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement
# the above approach
 
# Function to get closest greater
# number with at most non zero digit
def closestgtNum(str):
     
    # Stores the closest greater number
    # with at most one non-zero digit
    res = "";
 
    # Stores length of str
    n = len(str);
 
    if (str[0] < '9'):
        res += (chr)(ord(str[0]) + 1);
    else:
 
        # Append 10 to the end
        # of resultant String
        res += (chr)('1');
        res += (chr)('0');
 
    # Append n-1 times '0' to the end
    # of resultant String
    for i in range(n - 1):
        res += ('0');
 
    return res;
 
# Driver Code
if __name__ == '__main__':
     
    str = "120";
 
    print(closestgtNum(str));
 
# This code is contributed by Amit Katiyar


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to get closest greater
// number with at most non zero digit
public static string closestgtNum(string str)
{
     
    // Stores the closest greater number
    // with at most one non-zero digit
    string res = "";
      
    // Stores length of str
    int n = str.Length;
      
    if (str[0] < '9')
    {
        res = res + (char)(str[0] + 1);
    }
    else
    {
         
        // Append 10 to the end
        // of resultant string
        res = res + '1';
        res = res + '0';
    }
      
    // Append n-1 times '0' to the end
    // of resultant string
    for(int i = 0; i < n - 1; i++)
    {
        res = res + '0';
    }
    return res;
}
 
// Driver code
static void Main()
{
    string str = "120";
     
    Console.WriteLine(closestgtNum(str));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出
200

时间复杂度: O(log 2 N)
辅助空间: O(log 10 N)

有效的方法:想法是将给定整数的第一个数字的值增加 1,并将结果字符串初始化为给定整数的第一个数字。最后,在结果字符串的末尾追加(N – 1) 0 s 并返回结果字符串。

  1. 初始化一个字符串,比如res以存储最接近的较大数字,其中 st 最多一位非零数字。
  2. 在所得的字符串的末尾0 S 第一追加值STR [0] + 1在所得到的字符串,然后追加(1 N)。
  3. 打印res的值

下面是上述方法的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to get closest greater
// number with at most non zero digit
string closestgtNum(string str)
{
    // Stores the closest greater number
    // with at most one non-zero digit
    string res = "";
     
    // Stores length of str
    int n = str.length();
     
     
    if(str[0] < '9') {
        res.push_back(str[0] + 1);
    }
    else{
         
        // Append 10 to the end
        // of resultant string
        res.push_back('1');
        res.push_back('0');
    }
     
    // Append n-1 times '0' to the end
    // of resultant string
    for(int i = 0; i < n - 1; i++)
    {
        res.push_back('0');
    }
    return res;
     
     
}
 
// Driver Code
int main()
{
    string str = "120";
    cout<

Java

// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to get closest greater
// number with at most non zero digit
static String closestgtNum(String str)
{
     
    // Stores the closest greater number
    // with at most one non-zero digit
    String res = "";
     
    // Stores length of str
    int n = str.length();
     
    if (str.charAt(0) < '9')
    {
        res += (char)(str.charAt(0) + 1);
    }
    else
    {
         
        // Append 10 to the end
        // of resultant String
        res += (char)('1');
        res += (char)('0');
    }
     
    // Append n-1 times '0' to the end
    // of resultant String
    for(int i = 0; i < n - 1; i++)
    {
        res += (char)('0');
    }
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "120";
     
    System.out.print(closestgtNum(str));
}
}
 
// This code is contributed by Amit Katiyar

蟒蛇3

# Python3 program to implement
# the above approach
 
# Function to get closest greater
# number with at most non zero digit
def closestgtNum(str):
     
    # Stores the closest greater number
    # with at most one non-zero digit
    res = "";
 
    # Stores length of str
    n = len(str);
 
    if (str[0] < '9'):
        res += (chr)(ord(str[0]) + 1);
    else:
 
        # Append 10 to the end
        # of resultant String
        res += (chr)('1');
        res += (chr)('0');
 
    # Append n-1 times '0' to the end
    # of resultant String
    for i in range(n - 1):
        res += ('0');
 
    return res;
 
# Driver Code
if __name__ == '__main__':
     
    str = "120";
 
    print(closestgtNum(str));
 
# This code is contributed by Amit Katiyar

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
     
// Function to get closest greater
// number with at most non zero digit
public static string closestgtNum(string str)
{
     
    // Stores the closest greater number
    // with at most one non-zero digit
    string res = "";
      
    // Stores length of str
    int n = str.Length;
      
    if (str[0] < '9')
    {
        res = res + (char)(str[0] + 1);
    }
    else
    {
         
        // Append 10 to the end
        // of resultant string
        res = res + '1';
        res = res + '0';
    }
      
    // Append n-1 times '0' to the end
    // of resultant string
    for(int i = 0; i < n - 1; i++)
    {
        res = res + '0';
    }
    return res;
}
 
// Driver code
static void Main()
{
    string str = "120";
     
    Console.WriteLine(closestgtNum(str));
}
}
 
// This code is contributed by divyeshrabadiya07

Javascript


输出
200

时间复杂度: O(log 10 N)
辅助空间: O(log 10 N)