- 论坛徽章:
- 0
|
本帖最后由 ypyouyp 于 2010-09-24 02:14 编辑
开发环境:
OS : Linux tiger 2.6.27.41-170.2.117.fc10.i686
Server: nginx/0.7.64
PHP: PHP Version 5.2.9
in.txt
1.2,4,5,7,11,14,17,20
2.2,4,5,7,9,11,13,14,15,17,19
程序第一,二题- <?php
- $fp = fopen("in.txt", 'r');
- while(!feof($fp)) {
- $buf = fgets($fp);//逐次读取每一条记录
- //echo $buf;
- if(empty($buf)) continue;
- $question = explode(".", $buf);
- $question_num = $question[0];
- echo "第{$question_num}题:初始数据是:{$question[1]}<br />";
- $data = explode(",", $question[1]);
- switch($question_num) {
- case 1:
- $new_data = rand(0, 20);//生成随机待插入的数
- echo "随机插入的新数是{$new_data}<br />";
- $begin = 0;
- $end = count($data);//获取数组的个数
- $data = insert_data($data, $new_data, $begin, $end);//返回一个新的有序数组
- show_data($data, $new_data);
- write_data($data);
- break;
- case 2:
- $new_data = rand(0, 20);//生成随机待插入的数
- echo "随机生成的待寻找的数是{$new_data}<br />";
- $begin = 0;
- $end = count($data);//获取数组的个数
- $index = binsearch($data, $new_data, $begin, $end);
- if($index == -1) {
- echo "在原数组中没有找到要搜索的数据 {$new_data}<br />";
- } else {
- echo "在原数组中,待搜索的数据 {$new_data} 的下标是 {$index}<br />";
- }
- write_data($data);
- break;
- default:break;
- }
- echo "<p></p>";
- }
- /* 生成一新的数据$new_data,将该数据有序的插入到原数组$data之中,
- * 二分查找待插入的数据应该所处的位置
- */
- function insert_data($data, $search_data, $begin, $end) {
- $mid = intval(($begin + $end)/2);
- while($mid != $begin) {
- if($data[$mid] > $search_data) {
- return insert_data($data, $search_data, $begin, $mid);
- } else
- if($data[$mid] < $search_data) {
- return insert_data($data, $search_data, $mid, $end);
- } else {
- //找到和待插入的数据一样的数据了,
- return generate_array($data, $mid, $search_data);
- }
- }
- //到达了他应该所处的位置$mid附近
- if($data[$mid] > $search_data) {
- return generate_array($data, $mid, $search_data);
- } else {
- return generate_array($data, $mid+1, $search_data);
- }
- }
- /*
- * 二分查找,$begin起始下标,$end结束下标s
- */
- function binsearch($data, $search_data, $begin, $end) {
- $mid = intval(($begin + $end)/2);
- while($mid <= $end) {
- if($data[$mid] > $search_data) {
- return binsearch($data, $search_data, $begin, $mid);
- } else
- if($data[$mid] < $search_data) {
- return binsearch($data, $search_data, $mid, $end);
- } else {
- return $mid;
- }
- }
- return -1;//没有找到
- }
- /*
- * 将新的数据合并到老数组之中,位于$index的位置
- */
- function generate_array($data, $index, $new_data) {
- $length = count($data);
- while($index < $length) {
- $data[$length] = $data[$length-1];
- --$length;
- }
- $data[$index] = $new_data;
- //print_r($data);
- return $data;
- }
- //将数据写入文件out.txt
- function write_data($data) {
- $fp = fopen("out.txt", "a");
- $i = 0;
- while(isset($data[$i])) {
- fwrite($fp, $data[$i].",");
- ++$i;
- }
- fwrite($fp,"\n");
- fclose($fp);
- }
- //输出数据
- function show_data($data, $new_data) {
- $i = 0;
- while(isset($data[$i])) {
- if($data[$i]==$new_data) {
- echo "<strong>$data[$i]</strong>,";
- } else {
- echo $data[$i].",";
- }
- ++$i;
- }
- }
- ?>
复制代码 http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=12899305&ptid=1788191
4:使用PHP描述冒泡排序和快速排序算法,对象可以是一个数组- <?php
- $a = array(2, 5, 3, 19, 15, 7, 18, 15, 33, 5);
- print_r("原数组为: <br />");
- print_r($a);
- print_r("<br />冒泡排序输出结果:<br />");
- print_r(bubble_sort($a));
- print_r("<br />快速排序输出结果:<br />");
- $m = quick_sort($a);
- print_r($m);
- function bubble_sort($a) {
- $num = count($a);//获取数组的最大下标边界
- //遍历 $num 次,每次获取一个最大值
- for($i = 0; $i < $num; ++$i) {
- //第$i次遍历,获得第 $num-$i-1 的值(在0--$num-$i-1 中最大)
- for($j = 1; $j < $num-$i; ++$j) {
- //如果靠前为位置的值较靠后的相邻的值大,进行交换,依次类推
- if($a[$j-1] > $a[$j]) {
- $tmp = $a[$j-1];
- $a[$j-1] = $a[$j];
- $a[$j] = $tmp;#
- }
- }
- }
- return $a;
- }
- function quick_sort($a) {
- $len = count($a);
- //如果数组只有一个元素,返回该数组
- if($len == 1) return $a;
- //如果数组中每个元素都相同, 返回该数组
- for($i = 0; $i < $len; ++$i) {
- if($a[0] == $a[$i]) {
- if($i == $len-1) return $a;
- } else {
- break;
- }
- }
- //获取合适的$key(取数组的中间值)
- $key = get_key($a, $len);
- $j = 0;
- $k = 0;
- $front = array();
- $back = array();
- //依照key,将数组分为小于等于$key和大于$key两组
- for($i = 0; $i < $len; ++$i) {
- if($a[$i] <= $key) {
- $front[$j] = $a[$i];
- ++$j;
- } else
- if($a[$i] > $key) {
- $back[$k] = $a[$i];
- ++$k;
- }
- }
- //对两个数组进行快速排序
- $m = quick_sort($front);
- $n = quick_sort($back);
- //合并派好序的$key
- return array_merge($m, $n);
- }
- //在一个数组中寻找大概的中间值,
- function get_key($a, $len) {
- $min = $a[0];
- $max = $a[0];
- for($i=0; $i < $len; ++$i) {
- if($a[$i] > $max) {
- $max = $a[$i];
- }
- if($a[$i] < $min) {
- $min = $a[$i];
- }
- }
- //取数组的最大最小值的平均值
- $mid = intval(($max+$min)/2);
- return $mid;
- }
- ?>
复制代码 http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=12898518&ptid=1788191
//第3题 杨辉三角
- <?php
- pascal_triangle(6);
- function pascal_triangle($order) {
- $i = 0;
- $tmp = array();
- //杨辉三角生成 至第 $order 层处停止
- while($i <= $order) {
- $j = 0;
- //生成第 $i+1 层
- while($j <= $i) {
- //将第 $i层的行首和行尾置为 1 输出,第一,二层均在此处生成
- if($j == 0 || $j == $i) {
- $data[$j] = 1;
- echo $data[$j]." ";
- } else {
- //处理第二层以后的非边界值的存储和输出
- //通过第二层的数据生成第三层的中间值并输出,并临时存储到$tmp[$j]
- $tmp[$j] = $data[$j-1] + $data[$j];
- echo $tmp[$j]." ";
- }
- ++$j;
- }
- //补全$tmp数组的边界,即$tmp完整存储第$i层的所有数据
- $tmp[0] = 1;
- $tmp[$i] = 1;
- //将补全后的$tmp 赋值给 $data ,以便进行下层运算
- $data = $tmp;
- echo "<br />";
- ++$i;
- }
- }
复制代码 //第8题 字母的概率出现算法
- <?php
- test();
- function test() {
- $count[0] = 0;
- $count[1] = 0;
- $count[2] = 0;
- $count[3] = 0;
- for($i = 0; $i < 10000; ++$i) {
- $letter = get_letter();
- switch($letter) {
- case 'A':
- ++$count[0];break;
- case 'B':
- ++$count[1];break;
- case 'C':
- ++$count[2];break;
- case 'D':
- ++$count[3];break;
- }
- //print_r($count);
- }
- print_r("<br />A:".$count[0]/10000);
- print_r("<br />B:".$count[1]/10000);
- print_r("<br />C:".$count[2]/10000);
- print_r("<br />D:".$count[3]/10000);
- }
- function get_letter() {
- $letter = rand(1,10);
- //A 的概率是 1次
- if($letter == 1) { return 'A'; }
- else
- //B的概率是 2次
- if($letter > 1 && $letter <= 3) {
- return 'B';
- } else
- if($letter >3 && $letter < 7) {
- return 'C';
- } else {
- return 'D';
- }
- }
- ?>
复制代码 字母的概率出现结果
A:0.097
B:0.1953
C:0.3046
D:0.4031
//第11题 百钱买百鸡
- <?php
- hands_money_chook();
- function hands_money_chook() {
- for($a = 0; $a <= 33; ++$a) {
- for($b = 0; $b < 20; ++$b) {
- if(3*$a + 5*$b + (100-$a-$b)/3 == 100) {
- echo "公鸡数量:".$a."-----";
- echo "母鸡数目:".$b."----";
- $c = 100-$a-$b;
- echo "小鸡数目:".$c."<br />";
- }
- }
- }
- }
- ?>
复制代码 公鸡数量:4-----母鸡数目:12----小鸡数目:84
公鸡数量:11-----母鸡数目:8----小鸡数目:81
公鸡数量:18-----母鸡数目:4----小鸡数目:78
公鸡数量:25-----母鸡数目:0----小鸡数目:75
http://bbs.chinaunix.net/redirec ... 09&ptid=1788191
//第14题 小球的路径长度
- <?php
- $route = ball_route(10);
- print_r("小球的总路径长度为:".$route);
- function ball_route($height) {
- $route = 0;
- $flag = 10/1000;//原高度的前分之一
- while($height > $flag) {
- $route = $height + $height * 0.7 + $route;
- $height = $height * 0.7;
- //print_r($route."----".$height."<br />");
- }
- return $route;
- }
- ?>
复制代码 小球的总路径长度为:56.621451049098
//第21题 创建多极目录
- <?php
- create_folder("php/asp/jsp");
- function createFolder($path) {
- create_folder(dirname($path));
- mkdir($path, 0777);
- }
- ?>
复制代码 //第24题 求三个数中的最大值
- <?php
- //print_r("<br />假设三个数分别为5, 28, 11 <br />");
- $value = get_max_num(5, 28, 11);
- print_r("三个数中的最大值是 :");
- print_r($value);
- function get_max_num($a, $b, $c) {
- $max = $a > $b ? $a : $b;//返回$a,$b中较大的
- return $c > $max ? $c : $max;//返回最大值
- }
- ?>
复制代码 三个数中的最大值是 :28
http://bbs.chinaunix.net/redirec ... 45&ptid=1788191
//第10题 桃子最少有多少个-----3124- <?php
- $sum = monkey_eat_peach(1, 5);
- print_r("<br />桃子总数至少为 : $sum");
- function monkey_eat_peach($base_peach, $times) {
- //假设经过5次分桃后,每个猴子可以分到一个桃
- $sum = $base_peach;//每个猴子分$times次,每次分的桃子数 $base_peach
- for ($i = 0; $i < $times; ++$i) {
- $sum = ($sum * 5 + 1) / 4 ;
- }
- //如果出现分数,就将第五次分后每个猴子分到的桃子数 加一
- if(strpos($sum, ".")) {
- return monkey_eat_peach($base_peach + 1, $times);
- } else {
- return $sum;
- }
- }
- ?>
复制代码 桃子总数至少为 : 3124
//第12题 猴子吃枣的问题- <?php
- $sum = monkey_eat_jujube(10);
- print_r("<br />枣子的总数为:");
- print_r($sum);
- function monkey_eat_jujube($day) {
- $sum = 1;
- for($i = $day-1; $i > 0; --$i) {
- $sum = ($sum+1) *2;
- }
- return $sum;
- }
- ?>
复制代码 枣子的总数为:1534
20、简述如何得到当前执行脚本路径,包括所得到参数。
说明:例如有一个脚本www.domain.com,传给他的参数有参数1,参数2,参数3….
print_r($_SERVER['REQUEST_URI']);
25、写出查询发贴数最多的十个人名字的SQL,利用下表:
members(id,username,posts,pass,email)- select username from members order by posts desc limit 10
复制代码 27、有一数组 $a=array(4,3,8,9,2);请将其重新排序(按从小到大的顺序列出)。
- <?php
- $a=array(4,3,8,9,2);
- print_r($a);
- function bubble_sort($a) {
- $num = count($a);//获取数组的最大下标边界
- //遍历 $num 次,每次获取一个最大值
- for($i = 0; $i < $num; ++$i) {
- //第$i次遍历,获得第 $num-$i-1 的值(在0--$num-$i-1 中最大)
- for($j = 1; $j < $num-$i; ++$j) {
- //如果靠前为位置的值较靠后的相邻的值大,进行交换,依次类推
- if($a[$j-1] > $a[$j]) {
- $tmp = $a[$j-1];
- $a[$j-1] = $a[$j];
- $a[$j] = $tmp;#
- }
- }
- }
- return $a;
- }
- ?>
复制代码 Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 8 [4] => 9 )
29、对于大流量的网站,您采用什么样的方法来解决访问量问题?
- 1.页面静态化
- 2 .数据库服务器与web服务器分开
- 3.使用memcache
复制代码 |
|