|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Jan 2009
Città: milano, ma anche brescia
Messaggi: 132
|
[java] mistero nella funzione...
o sono stupido o non capisco la matematica di questo pezzo di codice:
public static int f(int x) { if (x==0||x==1) return x; return f(x/2) + f(x%2) - x; } se io alla funzione metto f(5) come fa a ritornarmi -5? a casa mia x/2 =2 , x%2=1. sapete spiegarmi questo mistero di java?
__________________
CPU: Amd Phenom II X2 X3 3 CORE SBLOCCATO 550 BE 3.5 ghz vcore 1.375 + Scythe Mugen 2 SCMG-2000 Rev. B MB: Asus M4A785TD-V EVO RAM: Team Group Elite DDR3 2x2gb 1333mhz CL9 ALI: LC-POWER Green Power LC6550GP V2.2 550W HD: Wd Caviar Blue 250gb 16MB 7200 RPM SATA GPU: Ati HD 4550 512MB DDR3 MONITOR: CRT 17" (1152x864) S.O.: Windows 7 Professional 64 Bit --- ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Jul 2008
Città: Solesino (PD)
Messaggi: 1252
|
f(2) = -1 per cui avresti f(2) + f(1) - 5 ovvero -1 +1 -5
__________________
Desktop: AMD FX-8350 @4,2Ghz + A.C. FREEZER XTREME rev.2 - ASUS M5A99X EVO - 32GB DDR3 (4x8GB) @1866Mhz - Sapphire NITRO+ RX 580 8GB - SSD Crucial MX500 1TB - Antec Two Hundred - Seasonic Focus+ Platinum 650W Always remember to K.I.S.S. In Vendita: Lenovo ThinkServer TS440 |
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: Jan 2009
Città: milano, ma anche brescia
Messaggi: 132
|
nono è un esercizio, cioè inserendo 5 il programma da come risultato -5
__________________
CPU: Amd Phenom II X2 X3 3 CORE SBLOCCATO 550 BE 3.5 ghz vcore 1.375 + Scythe Mugen 2 SCMG-2000 Rev. B MB: Asus M4A785TD-V EVO RAM: Team Group Elite DDR3 2x2gb 1333mhz CL9 ALI: LC-POWER Green Power LC6550GP V2.2 550W HD: Wd Caviar Blue 250gb 16MB 7200 RPM SATA GPU: Ati HD 4550 512MB DDR3 MONITOR: CRT 17" (1152x864) S.O.: Windows 7 Professional 64 Bit --- ![]() |
![]() |
![]() |
![]() |
#4 | |
Senior Member
Iscritto dal: Sep 2005
Città: Torino
Messaggi: 606
|
Quote:
Se guardi bene l'ultima riga di codice, puoi notare due chiamate alla funzione f stessa (ricorsione). anche a casa mia x/2=2 e x%2=1 con x=5, ma nel codice che hai postato devi poi calcolarne la f.
__________________
"Se proprio dovete piratare un prodotto, preferiamo che sia il nostro piuttosto che quello di qualcun altro." [Jeff Raikes] "Pirating software? Choose Microsoft!" |
|
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
da f(5)
hai f(2) + f(1) - 5 // -1 + 1 -5 da f(2) hai f(1) + f(0) - 2 // 1 + 0 - 2 = -1 te capì? |
![]() |
![]() |
![]() |
#6 |
Member
Iscritto dal: Jan 2009
Città: milano, ma anche brescia
Messaggi: 132
|
grazie 10 000, avevo completamente dimenticato che esistono le ricorsioni XDXDXDXD
__________________
CPU: Amd Phenom II X2 X3 3 CORE SBLOCCATO 550 BE 3.5 ghz vcore 1.375 + Scythe Mugen 2 SCMG-2000 Rev. B MB: Asus M4A785TD-V EVO RAM: Team Group Elite DDR3 2x2gb 1333mhz CL9 ALI: LC-POWER Green Power LC6550GP V2.2 550W HD: Wd Caviar Blue 250gb 16MB 7200 RPM SATA GPU: Ati HD 4550 512MB DDR3 MONITOR: CRT 17" (1152x864) S.O.: Windows 7 Professional 64 Bit --- ![]() |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:21.