Advertisement
desislava_topuzakova

03. Combinations

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