Advertisement
kirya_shkolnik

Java???

Jan 14th, 2024
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class CompareNumbers {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         System.out.println("Введите первое число:");
  8.         double number1 = scanner.nextDouble();
  9.  
  10.         System.out.println("Введите второе число:");
  11.         double number2 = scanner.nextDouble();
  12.  
  13.         System.out.println("Введите третье число:");
  14.         double number3 = scanner.nextDouble();
  15.  
  16.         if (number1 == number2 && number2 == number3) {
  17.             System.out.println("3");
  18.         } else if (number1 == number2 || number2 == number3 || number1 == number3) {
  19.             System.out.println("2");
  20.         } else {
  21.             System.out.println("1");
  22.         }
  23.     }
  24. }
  25.  
  26.  
  27. ///////////////////////////////////////
  28.  
  29. import java.util.Scanner;
  30.  
  31. public class FactorialSequence {
  32.     public static void main(String[] args) {
  33.         Scanner scanner = new Scanner(System.in);
  34.  
  35.         System.out.println("Введите вещественное число a:");
  36.         double a = scanner.nextDouble();
  37.  
  38.         double sum = 0;
  39.         int n = 1;
  40.  
  41.         while (true) {
  42.             double element = factorial(n) / calculateSum(n);
  43.             sum += element;
  44.  
  45.             if (element > a) {
  46.                 System.out.println("Первый элемент последовательности, больший a: " + element);
  47.                 break;
  48.             }
  49.  
  50.             n++;
  51.         }
  52.     }
  53.  
  54.     private static double factorial(int n) {
  55.         if (n == 0 || n == 1) {
  56.             return 1;
  57.         }
  58.         return n * factorial(n - 1);
  59.     }
  60.  
  61.     private static double calculateSum(int n) {
  62.         double sum = 0;
  63.         for (int i = 1; i <= n; i++) {
  64.             sum += i;
  65.         }
  66.         return sum;
  67.     }
  68. }
  69.  
  70. /////////////////////////////////////////////////
  71.  
  72. import java.util.Scanner;
  73. import java.util.HashSet;
  74. import java.util.Set;
  75.  
  76. public class UniqueElements {
  77.     public static void main(String[] args) {
  78.         Scanner scanner = new Scanner(System.in);
  79.  
  80.         System.out.println("Введите количество элементов массива:");
  81.         int n = scanner.nextInt();
  82.  
  83.         int[] array = new int[n];
  84.  
  85.         System.out.println("Введите значения элементов массива:");
  86.  
  87.         for (int i = 0; i < n; i++) {
  88.             array[i] = scanner.nextInt();
  89.         }
  90.  
  91.         int uniqueCount = countUniqueElements(array);
  92.  
  93.         System.out.println("Количество различных значений элементов массива: " + uniqueCount);
  94.     }
  95.  
  96.     private static int countUniqueElements(int[] array) {
  97.         Set<Integer> uniqueSet = new HashSet<>();
  98.  
  99.         for (int element : array) {
  100.             uniqueSet.add(element);
  101.         }
  102.  
  103.         return uniqueSet.size();
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement