cucumber Posted June 26, 2009 Report Share Posted June 26, 2009 Isti pareizi nevaru izdomat, ka var sadalit skaitlus grupas (intervalos), pienemsim pa 5. Sanak ka skaitli tiek iedaliti piecas grupas, ja skaitlu pietrukst, lai sadalitu grupa, tad shi grupa ar skaitliem paliek tuksha. Grupa 5 ir skaitli vislielako skaitli utt, kamer grupa 1 ir mazaki skaitli no kopejiem skaitliem. //TEST 1 $numbers = array(11,2,3,6,12,7,3,43,12); $min = min($numbers); $max = max($numbers); $div = ceil(($min+$max+1)/5); foreach ($numbers as $number) echo ceil(($number)/$div)."\n"; //2 1 1 1 2 1 1 5 2 //TEST 2 $numbers = array(11,2); $min = min($numbers); $max = max($numbers); $div = ceil(($min+$max+1)/5); foreach ($numbers as $number) echo ceil(($number)/$div)."\n"; //4 1 te it ka vajadzetu but 5 un 1 //TEST 3 //kaut kas uz to pusi bet ari nav tas $numbers = array(11,2,3,6,12,7,3,43,12); sort($numbers); print_r(array_chunk($numbers, 5)); Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted June 26, 2009 Report Share Posted June 26, 2009 Es laikam rīkotos kā Tavā 3. piemērā: $cik_grupa = 5; $numbers = array(11,2,3,6,12,7,3,43,12); sort($numbers); $cik_grupas_sanak = floor(sizeof($numbers)/$cik_grupa) $grupas = array(); for($x=0;$x<$cik_grupa_sanak;$x++){ $grupas[] = array_slice($numbers,$x * $cik_grupa,$cik_grupa); } print_r($grupas); Quote Link to comment Share on other sites More sharing options...
cucumber Posted June 26, 2009 Author Report Share Posted June 26, 2009 Tava piemera bik kludas. Es laikam rīkotos kā Tavā 3. piemērā: Sanak atpakal es dabuju 5 grupas, bet katra ir pa vienam elemetam, bet vajadzetu visus elemtus sadalit pa attiecigam grupam. //izlabotas $cik_grupa = 5; $numbers = array(11,2,3,6,12,7,3,43,12); sort($numbers); $cik_grupas_sanak = floor(sizeof($numbers)/$cik_grupa); $grupas = array(); for($x=0;$x<$cik_grupas_sanak;$x++){ $grupas[] = array_slice($numbers,$x * $cik_grupa,$cik_grupa); } print_r($grupas); Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted June 26, 2009 Report Share Posted June 26, 2009 nu itkā array_slice vajadzēja atgriezt $cik_grupa garus apakšmasīvus. Un apakšmasīvam šajā piemērā būtu jābūt 1, jo masīva garums ir 9 - tātad ieiet tikai 1 pilna apakšgrupa. Quote Link to comment Share on other sites More sharing options...
cucumber Posted June 26, 2009 Author Report Share Posted June 26, 2009 Ideja ir tada, ka var but ari tuksha ta grupa, vinai pilnai nav jabut vienmer. nu itkā array_slice vajadzēja atgriezt $cik_grupa garus apakšmasīvus. Un apakšmasīvam šajā piemērā būtu jābūt 1, jo masīva garums ir 9 - tātad ieiet tikai 1 pilna apakšgrupa. Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted June 26, 2009 Report Share Posted June 26, 2009 Ok - īsti nesaprotu sāpi... Lūdzu uzraksti, kā jāizskatās šīm grupām pie masīva: $numbers = array(11,2,3,6,12,7,3,43,12); Quote Link to comment Share on other sites More sharing options...
cucumber Posted June 26, 2009 Author Report Share Posted June 26, 2009 1. grupa 2,3,3 2. grupa 6,7,11,12 3.grupa - 4. grupa - 5. grupa 43 Ok - īsti nesaprotu sāpi... Lūdzu uzraksti, kā jāizskatās šīm grupām pie masīva: $numbers = array(11,2,3,6,12,7,3,43,12); Tads vel piemers. Ja butu skaitli 1,2,3,4,5, tad grupas butu 1.gr - 1 , 2.gr - 2 .. 5.gr - 5 Es to iedomajos, ka skaitlu virkni augosha seciba, kas ir sadalita piecas dala ar vienotu vertibu intervalu. Quote Link to comment Share on other sites More sharing options...
laacz Posted June 26, 2009 Report Share Posted June 26, 2009 1. grupa 2,3,3 2. grupa 6,7,11,12 3.grupa - 4. grupa - 5. grupa 43 Kālab otrajā grupā ir 6 un 7? Vai tad intervālu no 2 līd 43, ieskaitot, sadalot 5 apakšintervālos tie nav [2..10], [11..19], [20..28], [29..37], [38..43(46)]? Quote Link to comment Share on other sites More sharing options...
cucumber Posted June 26, 2009 Author Report Share Posted June 26, 2009 Jap laacz, tev bus taisniba Kālab otrajā grupā ir 6 un 7? Vai tad intervālu no 2 līd 43, ieskaitot, sadalot 5 apakšintervālos tie nav [2..10], [11..19], [20..28], [29..37], [38..43(46)]? Quote Link to comment Share on other sites More sharing options...
laacz Posted June 26, 2009 Report Share Posted June 26, 2009 Jap laacz, tev bus taisniba Elementāri, Mendelson (ja tur nav kļūdu, protams): Quote Link to comment Share on other sites More sharing options...
cucumber Posted June 26, 2009 Author Report Share Posted June 26, 2009 Jap patesteju ari uz saviem piemeriem, it ka strada ok. Paldies. Elementāri, Mendelson (ja tur nav kļūdu, protams): Radas vel viena ideja, pameginashu rit realizet. Quote Link to comment Share on other sites More sharing options...
cucumber Posted June 26, 2009 Author Report Share Posted June 26, 2009 Sanak mazak lietot, f-jas un dalishanu, strada aptuveni tapatas. $data = array(11,2,3,6,12,7,3,43,12); $max = max($data); while ($max % 5 != 0 ) { $max++; } $step = $max / 5; foreach($data as $value) { if ($value >= $step * 0 && $value <= $step * 1 ) { $result[1][] = $value; continue;} if ($value >= $step * 1 && $value <= $step * 2 ) { $result[2][] = $value; continue;} if ($value >= $step * 2 && $value <= $step * 3 ) { $result[3][] = $value; continue;} if ($value >= $step * 3 && $value <= $step * 4 ) { $result[4][] = $value; continue;} if ($value >= $step * 4 && $value <= $step * 5 ) { $result[5][] = $value; continue;} } print_r($result); Quote Link to comment Share on other sites More sharing options...
Aleksejs Posted June 26, 2009 Report Share Posted June 26, 2009 Nu, nez... Man šķiet, ka laacz variants ir labāks. Bez tam neesmu pārliecināts vai šis pēdējais variants vispār darbojas korekti. Ko tieši panāk šīs rindiņas? while ($max % 5 != 0 ) { $max++; } Jautājums atsaukts :) Quote Link to comment Share on other sites More sharing options...
bubu Posted June 26, 2009 Report Share Posted June 26, 2009 To var uzrakstīt arī bez while cikla: $max = ((($max + 4)/5)*5; Quote Link to comment Share on other sites More sharing options...
cucumber Posted June 27, 2009 Author Report Share Posted June 27, 2009 (edited) Man kaut ka neliekas korekti, pienemsim ka max ir skaitlis ir 3, tad 3+4 = 7 / 5 = 1,4 * 5 = 7 //bezjega sanak dalit un tad atpakal reizinat? To var uzrakstīt arī bez while cikla: $max = ((($max + 4)/5)*5; Edited June 27, 2009 by cucumber Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.