Advertisement
Guest User

Rekursion

a guest
Dec 5th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. // write your code here
  7. int[] arr = {-2,3,4,5};
  8. System.out.println(countPositives(arr, 0,3));
  9. char[] arr1 = {'a','b','c','d'};
  10. char[] arr2 = {'a','b','c','d'};
  11. System.out.println(contentCheck(arr1,arr2,3));
  12. }
  13.  
  14. public static int countPositives(int[] arr, int d, int t){
  15.  
  16. if(d <= t && t < arr.length){
  17. if(arr[t] > 0){
  18. return 1 + countPositives(arr, d, t-1);
  19. }
  20. else return 0 + countPositives(arr, d, t-1);
  21. }
  22. return 0;
  23. }
  24.  
  25. public static boolean contentCheck(char[] arr1, char[] arr2, int i ){
  26. if(i>0 && i<arr1.length && i<arr2.length){
  27. return (arr1[i] == arr2[i]) && contentCheck(arr1, arr2,i-1);
  28. }
  29. else if (i==0){
  30. return (arr1[i] == arr2[i]);
  31. }
  32. else return false;
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement