Advertisement
desislava_topuzakova

03. Combinations

Jun 4th, 2023
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 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.  
  8. //1. комбинация от три числа
  9. //първо число: 1 до n
  10. //второ число: 1 до n
  11. //трето число: 1 до n
  12.  
  13. int count = 0; //брой на валидни комбинации
  14. for (int first = 0; first <= n; first++) {
  15. for (int second = 0; second <= n; second++) {
  16. for (int third = 0; third <= n; third++) {
  17. //комбинацията от три числа: first; second; third
  18. int sum = first + second + third;
  19. if (sum == n) {
  20. //валидна комбинация
  21. count++;
  22. }
  23. }
  24. }
  25. }
  26.  
  27. //обходила всички комбинации
  28. System.out.println(count);
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement