Top.Mail.Ru
Ответы

Нужно написать Java код на NetBeans IDE 8.2, пожалуйста

Условие - Определить количество повторений каждой из цифр
0,1,2,...,9 в числе n!, где n – натуральное число, n>100. Нужно сделать путем разбивания числа на отдельные цифры, и потом через условие

По дате
По Рейтингу
Аватар пользователя
Новичок
12345678910111213141516171819202122232425262728
 import java.math.BigInteger; 
import java.util.Arrays; 
import java.util.Scanner; 
 
public class Q232541609 { 
 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 
        int n = scanner.nextInt(); 
        int[] repeats = new int[10]; 
        var factorial = factorial(n); 
        while (factorial.compareTo(BigInteger.valueOf(0)) > 0) { 
            int i = factorial.mod(BigInteger.valueOf(10)).intValue(); 
            factorial = factorial.divide(BigInteger.valueOf(10)); 
            repeats[i]++; 
        }  
        System.out.println("[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9]");
        System.out.println(Arrays.toString(repeats)); 
    } 
 
    private static BigInteger factorial(int n) { 
        var f = BigInteger.valueOf(1); 
        for (int i = 2; i <= n; i++) { 
            f = f.multiply(BigInteger.valueOf(i)); 
        } 
        return f; 
    } 
} 
Аватар пользователя
Мудрец

to find the number of repetitions.

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
System.out.print("Enter a natural number greater than 100: ");
int n = in.nextInt();
int[] count = new int[10];
// Calculate n!
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
// Split the number into individual digits
int temp = factorial;
while (temp > 0) {
int digit = temp % 10;
count[digit]++;
temp /= 10;
}
// Output the number of repetitions for each digit
System.out.println("The number of repetitions of each of the digits 0,1,2,...,9 in the number " + n + "! is:");
for (int i = 0; i < 10; i++) {
System.out.println(i + ": " + count[i]);
}
}
}

Аватар пользователя
Искусственный Интеллект

в NetBeans IDE 8.2 какая-то особая джава?