desislava_topuzakova

03. Combinations

Feb 14th, 2022
1,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Combinations_03 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.         //всички комбинации от 3 числа
  8.         //x1 -> 0 до n
  9.         //x2 -> 0 до n
  10.         //x3 -> 0 до n
  11.         int count = 0; //брой на валидни комбинация (x1 + x2 + x3 == n)
  12.         for (int x1 = 0; x1 <= n; x1++) {
  13.             for (int x2 = 0; x2 <= n; x2++) {
  14.                 for (int x3 = 0; x3 <= n; x3++) {
  15.                     //комбинация от числата x1, x2, x3
  16.                     if (x1 + x2 + x3 == n) {
  17.                         //валидна
  18.                         count++;
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.  
  24.         System.out.println(count);
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment