
Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
<?php
/**
* @version 1.0: 11407 Jun 12, 2012 9:52:51 PM willebil $
* @package Opteam
* @copyright Copyright (C) 2010 Opteam Solutions. All rights reserved.
* @license GNU GPL license v3., seehttp://www.gnu.org/copyleft/gpl.html
* author roma_i387
*/
const MATRIX_SIZE = 6;
/*
7. В прямоугольной матрице с элементами типа string найти отрезок массива наибольшей длины,
в котором элементы возрастают. Выведите отрезок и длину отрезка
*/
function example1() {
function draw($arr) {
print_r('<div style="clear: both;"></div>');
print_r('<br><hr>');
foreach ($arr as $row) {
if (is_array($row)) {
print_r('<div style="clear: both;"></div>');
foreach ($row as $item) {
print_r('<div style="float: left; width: 30px; text-align: center;">' . $item . '</div>');
}
} else {
print_r('<div style="float: left; width: 30px; text-align: center;">' . $row . '</div>');
}
}
print_r('<div style="clear: both;"></div>');
}
$arr = array();
$result = array();
for ($i = 0; $i < MATRIX_SIZE; $i++)
for ($j = 0; $j < MATRIX_SIZE; $j++) {
$arr[$i][$j] = (string) rand(0, 100);
$result[$i][$j] = 0;
}
draw($arr);
$tmp = array();
foreach ($arr as $row) {
foreach ($row as $item) {
$tmp[] = $item;
}
}
$output = array();
$result = array();
$maxLength = 0;
for ($i = 0; $i < count($tmp); $i++) {
$output[] = $tmp[$i];
for ($j = $i; $j < count($tmp) - 1; $j++) {
if ($tmp[$j] < $tmp[$j + 1]) {
$output[] = $tmp[$j + 1];
} else {
if (count($output) > $maxLength) {
$maxLength = count($output);
$result = $output;
}
$output = array();
$output[] = $tmp[$j + 1];
}
}
$output = array();
}
draw($result);
printf("Length = %d", $maxLength);
}
/*7. Написать функцию вычисления НОД(a, b).*/
function example2() {
if (!isset($_GET['_a']) || !isset($_GET['_b'])) {
die("Please set a and b");
}
$a = $_GET['_a'];
$b = $_GET['_b'];
while ($a != $b) {
if ($a > $b) {
$a -= $b;
} else {
$b -= $a;
}
}
printf("NOD for %d and %d is %d", $_GET['_a'], $_GET['_b'], $a);
}
/*7. Стихотворный текст (в строке не более 80 символов) имеет четырехстрочную строфу.
* Записать его «лесенкой» (по одному слову в строке), вставляя пустую строку по-сле каждого четверостишья.*/
function example3() {
$str = 'Стихотворный текст (в строке не более 80 символов) имеет четырехстрочную строфу. Записать его «лесенкой» (по одному слову в строке), вставляя пустую строку по-сле каждого четверостишья.';
$index = 0;
foreach(explode(' ', $str) as $word) {
if ($index == 4) {
$index = 0;
print_r('<div style="float: left; width: 100%; height: 20px;"></div>');
}
print_r('<div style="margin-left: ' . (20 * $index) . 'px">' . $word . '</div>');
$index++;
}
}
$action = isset($_GET['_action']) ? $_GET['_action'] : '';
$function = 'example' . $action;
if (!function_exists($function)) {
die('Please set action');
}
$function();