שאלה ברקורסיה

pitbol3

New member
שאלה ברקורסיה

אהלן,

אני צריך לכתוב פונקציה רקורסיבית, שתקבל כארגומנט מערך ואורכו, ותחזיר עבור כל איבר במערך האם הוא Well placed או לא.

הדרישות עבור well placed :

Example: Consider arr={0,5,1,1,2}. Indices 0,2 and 3 are well-placed:
• Index 0 is well-placed since P0=0.
• Index 1 is not well-placed, since P1=5>1. Since all elements are positive, it’s impossible to find a sequence starting at index 1 that can be summed to 1.
• Index 2 is well-placed since P2+P3= 2.
• Index 3 is well-placed, since P3 + P4 = 3.
• Index 4 is not well-placed: We can’t find a sequence starting at index that can be summed to 4 – that’s because P4 is the last element in the array, and it’s only 2.

עכשיו, השאלה שלי היא כזאת: כדי לעשות את זה באופן רקורסיבי(ללא לולאות או משתנים סטאטיים וכו'), ההגיון אומר שאת התרגיל הזה קשה לעשות מהסוף להתחלה,

כלומר קשה להתחיל מאורך 5, ועדיף להתחיל מ 0. השאלה שלי היא איך אפשרי לעשות את זה? אני לא רואה כאן דרך שאפשר לעשות את זה באופן "הפוך"?


תודה וחג שמח :)
 

pitbol3

New member
בכמה מילים לגבי התנאים

א. אם מתקיים arr==i אזי arr[i הוא wp.
ב. אם מתקיים arr+ arr[i+1] +...+[arr[j] ==i אזי arr[i הוא wp.
 
למעלה