Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [rekurencja] problem z kolejnym wywołaniem
bobek358
post 5.09.2014, 21:32:06
Post #1





Grupa: Zarejestrowani
Postów: 143
Pomógł: 22
Dołączył: 17.11.2007

Ostrzeżenie: (0%)
-----


Witam


Mam problem z pewną rekurencyjna funkcją.

Ogólnie zadaniem kodu jest wyszukanie rozwiązania (sumy) dającą zadaną liczbę z podanego zbioru.
Jeśli znajdzie taką kombinację to zwraca mi te liczy, które składają się na sumę i kończy szukanie kolejnych kombinacji.

Teraz chciałem wywołać ją ponownie, ale najpierw usuwam te rekordy z tablicy, które zostały użyte i mam problem z błędami notice.
Gdy wywalę kod 77 - 81 to wszystko jest ok, ale wtedy będzie brał mi pod uwagę te same wartości a tego nie chce.

Wynikiem działania poniższego kodu jest:
  1. Notice: Undefined variable: result in C:\xampp\htdocs\konkurs\index11.php on line 39
  2. 3,
  3.  
  4.  
  5. Notice: Undefined variable: result in C:\xampp\htdocs\konkurs\index11.php on line 39
  6.  
  7. Notice: Undefined offset: 2 in C:\xampp\htdocs\konkurs\index11.php on line 26
  8.  
  9. Notice: Undefined offset: 4 in C:\xampp\htdocs\konkurs\index11.php on line 26
  10. 4,


Wygląda to jak by php pamiętał pierwszą zadaną tablicę i miał gdzieś, że w kolejnym wywołaniu podaję mu już inną tablicę.
Macie pomysł jak to rozwiązać?


  1. <?php
  2. ini_set('display_errors', true);
  3. error_reporting(E_ALL | E_STRICT);
  4.  
  5. function combine($array, $val, $from = 0)
  6. {
  7.  
  8. static $level = 1;
  9.  
  10. static $result = array();
  11.  
  12. $cnt = count($array);
  13.  
  14. $results = array();
  15.  
  16. for($i = $from;$i < $cnt;$i++) {
  17. if($level < $val ) {
  18. $result[] = $array[$i];
  19. $from++;
  20. $level++;
  21. $results = array_merge($results, combine($array, $val, $from));
  22. $level--;
  23. array_pop($result);
  24. }
  25. else {
  26. $result[] = $array[$i];
  27. $results[] = $result;
  28. array_pop($result);
  29. }
  30. }
  31. return $results;
  32. }
  33.  
  34. function search ($strings, $need)
  35. {
  36. $strings_count = count($strings);
  37. for ($i = 1; $i <= $strings_count; $i++)
  38. {
  39. if ($need == $result)
  40. break;
  41.  
  42. $combination = combine($strings, $i, 0);
  43.  
  44. foreach ($combination as $tab)
  45. {
  46. $result = 0;
  47. unset($orders);
  48. foreach ($tab as $all)
  49. {
  50. $result += $all;
  51. $orders[] = $all;
  52. }
  53.  
  54. if ($need == $result)
  55. {
  56. return $orders;
  57. break;
  58. }
  59. }
  60. }
  61. return NULL;
  62. }
  63.  
  64. $result = 3989;
  65. $orders = array(1,2,3,4,5,6,7);
  66.  
  67. $wynikowa_tab = search ( $orders, 3);
  68.  
  69. if ($wynikowa_tab != NULL)
  70. {
  71. foreach ($wynikowa_tab as $tab)
  72. {
  73. echo $tab.",";
  74. }
  75. }
  76.  
  77. $key_del = array_search(3, $orders);
  78. unset($orders[$key_del]);
  79.  
  80. $key_del = array_search(5, $orders);
  81. unset($orders[$key_del]);
  82.  
  83. echo "<br><br>";
  84.  
  85. $wynikowa_tab = search ( $orders, 4);
  86.  
  87. if ($wynikowa_tab != NULL)
  88. {
  89. foreach ($wynikowa_tab as $tab)
  90. {
  91. echo $tab.",";
  92. }
  93. }
  94. ?>
Go to the top of the page
+Quote Post
pedro84
post 5.09.2014, 22:01:48
Post #2





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

Ostrzeżenie: (0%)
-----


Poczytać: http://php.net/manual/en/language.variables.scope.php.


--------------------
Google knows the answer...
Go to the top of the page
+Quote Post
bobek358
post 5.09.2014, 22:37:42
Post #3





Grupa: Zarejestrowani
Postów: 143
Pomógł: 22
Dołączył: 17.11.2007

Ostrzeżenie: (0%)
-----


Rozumiem, że chodzi Ci o zmienne statyczne w pierwszej funkcji?

Edit.

Ok wiem już co zrobiłem nie tak, po usunięciu tych 2 rekordów brakowało ciągłości w indeksach.
Przepisałem tablicę od nowa, aby nadać kolejne indeksy po usunięciu tych elementów i wszystko gra smile.gif

Ten post edytował bobek358 5.09.2014, 23:05:37
Go to the top of the page
+Quote Post
pedro84
post 5.09.2014, 23:06:15
Post #4





Grupa: Nieautoryzowani
Postów: 2 249
Pomógł: 305
Dołączył: 2.10.2006

Ostrzeżenie: (0%)
-----


Konkretnie o to
  1. if ($need == $result)
z funkcji search.

Edit: no to git wink.gif

Ten post edytował pedro84 5.09.2014, 23:06:30


--------------------
Google knows the answer...
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 24.06.2025 - 06:08