📌  相关文章
📜  八进制数字系统中最大的偶数和奇数N位数字

📅  最后修改于: 2021-04-30 02:36:00             🧑  作者: Mango

给定整数N ,任务是在八进制数字系统中找到最大的偶数和奇数N位数字。
例子:

方法:要获得最大的数字,该数字必须尽可能多。由于在八进制数字系统中,最大位数为‘7’ 。因此,生成‘7’ (N – 1)次,然后在末尾附加‘6’表示偶数和‘7’表示奇数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the largest n-digit even
// and odd numbers in octal number system
void findNumbers(int n)
{
 
    // Append '7' (N - 1) times
    string ans = string(n - 1, '7');
 
    // Append '6' for an even number
    string even = ans + '6';
 
    // Append '7' for an odd number
    string odd = ans + '7';
 
    cout << "Even : " << even << endl;
    cout << "Odd : " << odd << endl;
}
 
// Driver code
int main()
{
    int n = 4;
 
    findNumbers(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to print the largest n-digit even
// and odd numbers in octal number system
static void findNumbers(int n)
{
 
    // Append '7' (N - 1) times
    String ans = "";
    for (int i = 0; i < n - 1; i++)
        ans += '7';
 
    // Append '6' for an even number
    String even = ans + '6';
 
    // Append '7' for an odd number
    String odd = ans + '7';
 
    System.out.println("Even : " + even);
    System.out.println("Odd : " + odd);
}
 
// Driver code
public static void main(String args[])
{
    int n = 4;
 
    findNumbers(n);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach ;
 
# Function to print the largest n-digit even
# and odd numbers in octal number system
def findNumbers(N) :
 
    # Append '7' (N - 1) times
    ans = '7' * (N - 1)
 
    # Append '6' for an even number
    even = ans + '6';
 
    # Append '7' for an odd number
    odd = ans + '7';
 
    print("Even : ", even);
    print("Odd : ", odd );
 
# Driver code
if __name__ == "__main__" :
 
    n = 4;
 
    findNumbers(n);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
// Function to print the largest n-digit even
// and odd numbers in octal number system
static void findNumbers(int n)
{
 
    // Append '7' (N - 1) times
    String ans = "";
    for (int i = 0; i < n - 1; i++)
        ans += '7';
 
    // Append '6' for an even number
    String even = ans + '6';
 
    // Append '7' for an odd number
    String odd = ans + '7';
 
    Console.WriteLine("Even : " + even);
    Console.WriteLine("Odd : " + odd);
}
 
// Driver code
public static void Main(String []args)
{
    int n = 4;
 
    findNumbers(n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Even : 7776
Odd : 7777

时间复杂度: O(n)