GarryOne Posted September 26, 2012 Report Share Posted September 26, 2012 (edited) De fapt se poate face in orice limbaj.Algoritmul este in felul urmator:Fiecare numar este suma numerelor din dreapta lui, mai jos de el, dreapt si mai jos de el.Exemplu: 13 = 5 + 5 + 3; 25 = 7 + 13 + 5;Sa fie generat acest tabel de numere pe baza unui numar introdus de la tastatura. In exemplul de mai sus, numarul introdus de la tastatura a fost 4, adica 4 coloane si 4 randuri.Am avut problema la info pe acasa, si am rezolvat-o, dar mi s-a parut interesanta.Solvers: Sim Master, R3AL, vpatrikv, ionut.hulub Edited September 28, 2012 by GarryOne 1 Quote Link to comment Share on other sites More sharing options...
Birkoff Posted September 26, 2012 Report Share Posted September 26, 2012 pai si de la ce numar se incepe? de la 1 sau de la cel introdus la tastatura? in poza pare sa se inceapa de la 1 Quote Link to comment Share on other sites More sharing options...
GarryOne Posted September 27, 2012 Author Report Share Posted September 27, 2012 pai si de la ce numar se incepe? de la 1 sau de la cel introdus la tastatura? in poza pare sa se inceapa de la 1de la 1. Quote Link to comment Share on other sites More sharing options...
Sim Master Posted September 27, 2012 Report Share Posted September 27, 2012 (edited) Nu stiu daca e si cea mai simpla metoda dar mi-a reusit:<?phpif (!isset($_GET['cols'])){ $_GET['cols'] = 4;}if (!isset($_GET['first'])){ $_GET['first'] = 1;}$col_rows = $_GET['cols'];$first_num = $_GET['first'];$total_cells = $col_rows*$col_rows; // ^2//Create rows and columns$cell_pos = 0;$row_pos = 0;for ($i = 0; $i<$total_cells; $i++){ if (($cell_pos == ($col_rows-1)) || ($row_pos == ($col_rows-1))) { $cell[$row_pos][$cell_pos] = $first_num; } else { $cell[$row_pos][$cell_pos] = null; } $cell_pos++; if ($cell_pos == $col_rows) { $cell_pos = 0; $row_pos++; }}//Calculating values$cell_pos = $col_rows-1;$row_pos = $col_rows-1;for ($i = $total_cells; $i> 0; $i--){ if (($cell[$row_pos][$cell_pos] == null) && ($cell[$row_pos+1][$cell_pos] != null) && ($cell[$row_pos+1][$cell_pos+1] != null) && ($cell[$row_pos][$cell_pos+1] != null)) { $cell[$row_pos][$cell_pos] = $cell[$row_pos+1][$cell_pos]+$cell[$row_pos+1][$cell_pos+1]+$cell[$row_pos][$cell_pos+1]; } $cell_pos--; if ($cell_pos < 0) { $cell_pos = $col_rows-1; $row_pos--; }}//Printing HTML Table$cell_pos = 0;$row_pos = 0;echo '<table border="1" style="font-size:24px;"><tr>';for ($i = 0; $i<$total_cells; $i++){ echo '<td width="80">'.$cell[$row_pos][$cell_pos].'</td>'; $cell_pos++; if ($cell_pos == $col_rows) { echo '</tr><tr>'; $cell_pos = 0; $row_pos++; }}echo '</tr></table>';?><br><br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">Table rows & columns: <input type="text" size="3" name="cols" value="4"><br>First number: <input type="text" size="3" name="first" value="1"><br><input type="submit" value="Submit"><br></form>Noob question: Cum fac sa scrie codu colorat cand il pun in [ code] [/ code]? Edited September 27, 2012 by Sim Master Quote Link to comment Share on other sites More sharing options...
Cril Posted September 27, 2012 Report Share Posted September 27, 2012 Nu stiu daca e si cea mai simpla metoda dar mi-a reusit:<?phpif (!isset($_GET['cols'])){ $_GET['cols'] = 4;}if (!isset($_GET['first'])){ $_GET['first'] = 1;}$col_rows = $_GET['cols'];$first_num = $_GET['first'];$total_cells = $col_rows*$col_rows; // ^2//Create rows and columns$cell_pos = 0;$row_pos = 0;for ($i = 0; $i<$total_cells; $i++){ if (($cell_pos == ($col_rows-1)) || ($row_pos == ($col_rows-1))) { $cell[$row_pos][$cell_pos] = $first_num; } else { $cell[$row_pos][$cell_pos] = null; } $cell_pos++; if ($cell_pos == $col_rows) { $cell_pos = 0; $row_pos++; }}//Calculating values$cell_pos = $col_rows-1;$row_pos = $col_rows-1;for ($i = $total_cells; $i> 0; $i--){ if (($cell[$row_pos][$cell_pos] == null) && ($cell[$row_pos+1][$cell_pos] != null) && ($cell[$row_pos+1][$cell_pos+1] != null) && ($cell[$row_pos][$cell_pos+1] != null)) { $cell[$row_pos][$cell_pos] = $cell[$row_pos+1][$cell_pos]+$cell[$row_pos+1][$cell_pos+1]+$cell[$row_pos][$cell_pos+1]; } $cell_pos--; if ($cell_pos < 0) { $cell_pos = $col_rows-1; $row_pos--; }}//Printing HTML Table$cell_pos = 0;$row_pos = 0;echo '<table border="1" style="font-size:24px;"><tr>';for ($i = 0; $i<$total_cells; $i++){ echo '<td width="80">'.$cell[$row_pos][$cell_pos].'</td>'; $cell_pos++; if ($cell_pos == $col_rows) { echo '</tr><tr>'; $cell_pos = 0; $row_pos++; }}echo '</tr></table>';?><br><br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">Table rows & columns: <input type="text" size="3" name="cols" value="4"><br>First number: <input type="text" size="3" name="first" value="1"><br><input type="submit" value="Submit"><br></form>Noob question: Cum fac sa scrie codu colorat cand il pun in [ code] [/ code]?folosesti [ php ] si [ / php ] Quote Link to comment Share on other sites More sharing options...
R3AL Posted September 27, 2012 Report Share Posted September 27, 2012 C++# include <iostream># include <iomanip>int main(int argc, char const *argv[]){ int n=4; std::cin>>n; int **table = new int*[n]; for(unsigned i = 0; i < n; ++i ) table[i] = new int[n]; for(unsigned i = 0; i < n; ++i ) { table[i][n-1] = 1; table[n-1][i] = 1; } for(int i = n-2; i >= 0; --i ) for(int j = n-2; j >= 0; --j ) table[i][j] = table[i+1][j] + table[i+1][j+1] + table[i][j+1]; for(unsigned i = 0; i < n; ++i ) { for(unsigned j = 0; j < n; ++j ) std::cout<<std::setw(n)<<table[i][j]; } for(unsigned i = 0; i < n; ++i ) delete[] table[i]; delete[] table; return 0;} Quote Link to comment Share on other sites More sharing options...
GarryOne Posted September 27, 2012 Author Report Share Posted September 27, 2012 (edited) Bravo Sim Master, R3AL Edited September 27, 2012 by GarryOne Quote Link to comment Share on other sites More sharing options...
SilviuSDS Posted September 27, 2012 Report Share Posted September 27, 2012 Frumos challenge, am sa aduc vorba maine despre problema asta la ora de info, sa vad daca e in stare profu sa o rezolve sau cineva din clasa, bineinteles inafara de mine, eu deja stiu algoritmul, iar sansele ca altcineva de la mine din clasa sa frecventeze rst sunt 0, deoarece sunt prea boi. Quote Link to comment Share on other sites More sharing options...
Cril Posted September 27, 2012 Report Share Posted September 27, 2012 Frumos challenge, am sa aduc vorba maine despre problema asta la ora de info, sa vad daca e in stare profu sa o rezolve sau cineva din clasa, bineinteles inafara de mine, eu deja stiu algoritmul, iar sansele ca altcineva de la mine din clasa sa frecventeze rst sunt 0, deoarece sunt prea boi.Si ce o sa rezolvi? Te lauzi cu codul/algoritmul gasit de altii? Niciodata nu am suportat colegii de genul tau, care se dadeau smart-asses si defapt nu stiau nimic sau stiau foarte putin.Un om poate fi bun intr-un domeniu in care altul este total paralel, dar asta este problema sistemului educational din tara noastra. Tu poate oi fi bun la informatica, in timp ce altul e bun la sport sau la romana. Si profii de info din liceu in general stiu lucruri elementare pe care vi le predau mot-a-mot, pentru ca niciun informatician bun nu va accepta sa predea intr-un liceu pe un salariu de cacat. Quote Link to comment Share on other sites More sharing options...
SilviuSDS Posted September 28, 2012 Report Share Posted September 28, 2012 (edited) Si ce o sa rezolvi? Te lauzi cu codul/algoritmul gasit de altii? Niciodata nu am suportat colegii de genul tau, care se dadeau smart-asses si defapt nu stiau nimic sau stiau foarte putin.Un om poate fi bun intr-un domeniu in care altul este total paralel, dar asta este problema sistemului educational din tara noastra. Tu poate oi fi bun la informatica, in timp ce altul e bun la sport sau la romana. Si profii de info din liceu in general stiu lucruri elementare pe care vi le predau mot-a-mot, pentru ca niciun informatician bun nu va accepta sa predea intr-un liceu pe un salariu de cacat.Eu nu ma laud, spun de unde am aflat de acest algoritm si care e sursa, doar ca profu meu de info e cam idiot si vreau sa-i dau de gandit, in clasa a-9-a a zis ca facem Pascal pentru ca el a uitat C++-ul, iar acum in ultimul an parca a uitat si Pascalul.Eu sunt la 12 B, cei de la 12 A fac cu o profesoara care chiar se pricepe foarte bine la ceea ce face, asa ca nu zit u ca nu sunt informaticieni buni care sa predea in scoli. Edited September 28, 2012 by SilviuSDS Quote Link to comment Share on other sites More sharing options...
vpatrikv Posted September 28, 2012 Report Share Posted September 28, 2012 O varianta poate putin mai simpla :<?php echo '<form action="index.php" method="GET"> Enter number: <input type="text" name="number"> <br/> <input type = "submit" value="Send"> </form>'; if(isset($_GET['number']) && !empty($_GET['number'])){ $number = $_GET['number']; $table =array(); for($i=0;$i<$number;$i++){ for($j=0;$j<$number;$j++){ if(($i == 0) || ($j == 0)){ $table[$i][$j]=1; }else{ $table[$i][$j] = $table[$i-1][$j] + $table[$i][$j-1] + $table[$i-1][$j-1]; } } } echo '<table>'; for($i=$number-1;$i>=0;$i--){ echo '<tr>'; for($j=$number-1;$j>=0;$j--){ echo '<td>'.$table[$i][$j].'</td>'; } echo '</tr>'; } echo '</table>'; }?> Quote Link to comment Share on other sites More sharing options...
ionut.hulub Posted September 28, 2012 Report Share Posted September 28, 2012 puts "Intrudu numarul de linii si coloane: "nrLinii = gets.strip.to_itabel = Array.new(nrLinii) {Array.new(nrLinii, 1)}tabel[nrLinii-2][nrLinii-2] = 3 #initializam tabelul(nrLinii-3).step(0, -1) do |i| tabel[i][i] = tabel[i+1][i+1] + (tabel[i+1][i+1] - tabel[i+2][i+2]) * 5end #calculam diagonala principala(nrLinii-3).step(0, -1) do |i| (nrLinii-2).step(i+1, -1) do |j| tabel[i][j] = tabel[j][i] = tabel[i+1][j] + tabel[i][j+1] + tabel[i+1][j+1] endend #adaugam restul elementelortabel.each do |linie| p linieend #afisam tabelulo varianta mai optimizata scrisa in rubyprima data calculez diagonala principala dupa formula a[j] = a[i+1][j+1] + (a[i+1][j+1] - a[i+2][j+2]) * 5dupa care calculez doar pentru jumatate de tabel pentru ca tabelul este simetric fata de diagonala principala. Quote Link to comment Share on other sites More sharing options...