• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

求教导怎么优化算法

php 搞代码 4年前 (2022-01-23) 11次浏览 已收录 0个评论

只是第1,2次选择。还要再做2次。后面基本一样重复第二次志愿的。100个学生运行数据大概花了6-7秒。求指导怎么可以优化下~~谢谢

if($_POST[‘cal’])
{
//——————————————–第一志愿——————————————-
$sql = “select * from hos;”;
$res = mysql_query($sql)or die(mysql_error());
$info = mysql_fetch_array($res)or die(mysql_error());
//echo $info[‘total’]; //得到总医院数
do{ //循环1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
if($info[‘same’]==0) //如果不分男女时
{
$hos_id = $info[‘id’]; //医院ID号
//echo $hos_id.”,”;
$hos_total = $info[‘total’]; //医院总需求人数
//echo $hos_total.”.”;
$sql_total = “select count(*) as total from stu where first='”.$hos_id.”‘ and get=0 ;”; //添该志愿的学生数
$res_total = mysql_query($sql_total);
$info_total = mysql_fetch_array($res_total); //得到填报该志愿的学生数
if($hos_total > $info_total[‘total’])
{$sql1 = “select id from stu where first='”.$hos_id.”‘ and get=0 ORDER BY score DESC ;”;}
else {$sql1 = “select id from stu where first='”.$hos_id.”‘ and get=0 ORDER BY score DESC limit $hos_total;”;}
$res1 = mysql_query($sql1);
$info1 = mysql_fetch_array($res1);
do //循环2
{
$stu_id = $info1[‘id’];
$sql2 = “UPDATE stu SET result='”.$hos_id.”‘,get=1 where id='”.$stu_id.”‘ ;”;
$res2 = mysql_query($sql2);
}while($info1 = mysql_fetch_array($res1)); //循环2结束
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线

else //如果区分男女—–$info[‘same’]==1;
{
$hos_id = $info[‘id’]; //医院ID号
//男生SQL
$hos_total_men = $info[‘men’]; //医院总需求男生数
$sql_total_men = “select count(*) as total_men from stu where first='”.$hos_id.”‘ and sex=’f’ and get=0 ;”; //添该志愿的男学生数
$res_total_men = mysql_query($sql_total_men);
$info_total_men = mysql_fetch_array($res_total_men); //得到填报该志愿的男学生数
if($hos_total_men > $info_total_men[‘total_men’])
{$sql1_men = “select id from stu where first='”.$hos_id.”‘ and sex=’m’ and get=0 ORDER BY score DESC ;”;}
else {$sql1_men = “select id from stu where first='”.$hos_id.”‘and sex=’m’ and get=0 ORDER BY score DESC limit $hos_total_men;”;}
$res1_men = mysql_query($sql1_men);
$info1_men = mysql_fetch_array($res1_men);
do //循环2
{
//男生更新
$stu_id_men = $info1_men[‘id’];
$sql2_men = “UPDATE stu SET result='”.$hos_id.”‘,get=1 where id='”.$stu_id_men.”‘ ;”;
$res2_men = mysql_query($sql2_men);
}while($info1_men = mysql_fetch_array($res1_men));
//女生SQL
$hos_total_wom = $info[‘wom’]; //医院总需求女生数
$sql_total_wom = “select count(*) as total_wom from stu where first='”.$hos_id.”‘ and sex=’f’ and get=0 ;”; //添该志愿的男学生数
$res_total_wom = mysql_query($sql_total_wom);
$info_total_wom = mysql_fetch_array($res_total_wom); //得到填报该志愿的女学生数
if($hos_total_wom > $info_total_wom[‘total_wom’])
{$sql1_wom = “select id from stu where first='”.$hos_id.”‘ and sex=’f’ and get=0 ORDER BY score DESC ;”;}
else {$sql1_wom = “select id from stu where first='”.$hos_id.”‘ and sex=’f’ and get=0 ORDER BY score DESC limit $hos_total_wom;”;}
$res1_wom = mysql_query($sql1_wom);
$info1_wom = mysql_fetch_array($res1_wom);
do //循环2
{
//女生更新
$stu_id_men = $info1_wom[‘id’];
$sql2_wom = “UPDATE stu SET result='”.$hos_id.”‘,get=1 where id='”.$stu_id_wom.”‘;”;
$res2_wom = mysql_query($sql2_wom);
}while($info1_wom = mysql_fetch_array($res1_wom));
//循环2结束
}

}while($info = mysql_fetch_array($res)); //循环1结束
//——————————————–第一志愿结束——————————————-

//———————————————-第二志愿———————————————
$s_sql = “select * from hos;”;
$s_res = mysql_query($s_sql)or die(mysql_error());
$s_info = mysql_fetch_array($s_res)or die(mysql_error());
do{ //循环1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
if($s_info[‘same’]==0) //如果不分男女时
{
$s_hos_id = $s_info[‘id’]; //医院ID号
$s_hos_total = $s_info[‘total’]; //医院总需求人数
$s_sql_total = “select count(*) as total from stu where second='”.$s_hos_id.”‘ and get=0 ;”; //添该志愿的学生数
$s_res_total = mysql_query($s_sql_total);
$s_info_total = mysql_fetch_array($s_res_total); //得到第二志愿填报该志愿的学生数
$s_get_sql = “select count(*) as total from stu where result='”.$s_hos_id.”‘ ;”; //得到第一志愿已为HOS_ID的学生
$s_get_res = mysql_query($s_get_sql);
$s_get_info = mysql_fetch_array($s_get_res);
if($s_get_info[‘total’] == $s_hos_total){}
else
{
$xuqiu2 = $s_hos_toal-$s_get_info[‘total’]; //还需要多少学生数
if( $xuqiu2 > $s_info_total[‘total’]){$s_sql1 = “select id from stu where second='”.$s_hos_id.”‘ and get=0 ORDER BY score DESC ;”;}
else {$s_sql1 = “select id from stu where second='”.$s_hos_id.”‘ and get=0 ORDER BY score DESC limit $xuqiu2;”;}
$s_res1 = mysql_query($s_sql1);
$s_info1 = mysql_fetch_array($s_res1);
do //循环2
{
$s_stu_id = $s_info1[‘id’];
$s_sql2 = “UPDATE stu SET result='”.$s_hos_id.”‘,get=1 where id='”.$s_stu_id.”‘ ;”;
$s_res2 = mysql_query($s_sql2);
}while($s_info1 = mysql_fetch_array($s_res1));
}
}//循环2结束

else //如果区分男女—–$info[‘same’]==1;
{
$s_hos_id = $s_info[‘id’]; //医院ID号
//男生SQL
$s_hos_total_men = $s_info[‘men’]; //医院总需求男生数
$s_sql_total_men = “select count(*) as total_men from stu where second='”.$s_hos_id.”‘ and sex=’m’ and get=0 ;”; //添该志愿的男学生数
$s_res_total_men = mysql_query($s_sql_total_men);
$s_info_total_men = mysql_fetch_array($s_res_total_men); //得到填报该志愿的男学生数

$s_get_sql_men = “select count(*) as total from stu where result='”.$s_hos_id.”‘ and sex=’m’ ;”; //得到第一志愿已为HOS_ID的男学生
$s_get_res_men = mysql_query($s_get_sql_men);
$s_get_info_men = mysql_fetch_array($s_get_res_men);
if($s_get_info_men[‘total’] == $s_hos_total_men){}
else
{
$xuqiu2_men = $s_hos_toal_men-$s_get_info_men[‘total’]; //还需要多少男学生数
if($xuqiu2_men > $s_info_total_men[‘total_men’])
{$s_sql1_men = “select id from stu where second='”.$s_hos_id.”‘ and sex=’m’ and get=0 ORDER BY score DESC ;”;}
else {$s_sql1_men = “select id from stu where second='”.$s_hos_id.”‘and sex=’m’ and get=0 ORDER BY score DESC limit $xuqiu2_men;”;}
$s_res1_men = mysql_query($s_sql1_men);
$s_info1_men = mysql_fetch_array($s_res1_men);
do //循环2
{
//男生更新
$s_stu_id_men = $s_info1_men[‘id’];
$s_sql2_men = “U本文@来#源gaodai$ma#com搞$$代**码网搞代gaodaima码PDATE stu SET result='”.$s_hos_id.”‘,get=1 where id='”.$s_stu_id_men.”‘ ;”;
$s_res2_men = mysql_query($s_sql2_men);
}while($s_info1_men = mysql_fetch_array($s_res1_men));
}

//女生SQL
$s_hos_total_wom = $s_info[‘wom’]; //医院总需求女生数
$s_sql_total_wom = “select count(*) as total_wom from stu where second='”.$s_hos_id.”‘ and sex=’f’ and get=0 ;”; //添该志愿的女学生数
$s_res_total_wom = mysql_query($s_sql_total_wom);
$s_info_total_wom = mysql_fetch_array($s_res_total_wom); //得到填报该志愿的女学生数

$s_get_sql_wom = “select count(*) as total from stu where result='”.$s_hos_id.”‘ and sex=’f’ ;”; //得到第一志愿已为HOS_ID的女学生
$s_get_res_wom = mysql_query($s_get_sql_wom);
$s_get_info_wom = mysql_fetch_array($s_get_res_wom);
if($s_get_info_wom[‘total’] == $s_hos_total_wom){}
else
{
$xuqiu2_wom = $s_hos_toal_wom-$s_get_info_wom[‘total’]; //还需要多少女学生数
if($xuqiu2_wom > $s_info_total_wom[‘total_wom’])
{$s_sql1_wom = “select id from stu where second='”.$s_hos_id.”‘ and sex=’f’ and get=0 ORDER BY score DESC ;”;}
else {$s_sql1_wom = “select id from stu where second='”.$s_hos_id.”‘and sex=’f’ and get=0 ORDER BY score DESC limit $xuqiu2_wom;”;}
$s_res1_wom = mysql_query($s_sql1_wom);
$s_info1_wom = mysql_fetch_array($s_res1_wom);
do //循环2
{
//女生更新
$s_stu_id_wom = $s_info1_wom[‘id’];
$s_sql2_wom = “UPDATE stu SET result='”.$s_hos_id.”‘,get=1 where id='”.$s_stu_id_wom.”‘ ;”;
$s_res2_wom = mysql_query($s_sql2_wom);
}while($s_info1_wom = mysql_fetch_array($s_res1_wom));
}

}

}while($info = mysql_fetch_array($res));


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:求教导怎么优化算法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址