Skip to content

Instantly share code, notes, and snippets.

@PichuChen
Last active August 29, 2015 14:22
Show Gist options
  • Save PichuChen/272f36eb26b0c1dcdd86 to your computer and use it in GitHub Desktop.
Save PichuChen/272f36eb26b0c1dcdd86 to your computer and use it in GitHub Desktop.
試著把Model和View分離之後,原先解決掉重覆生成的效能就被吃掉的第二版。
<?php
// 100 以內加減法正整數答案
// 美觀度就別管了XD
header('Content-Type: text/html; charset=utf-8');
ini_set('memory_limit', '1024M');
ini_set('display_errors', 1);
ini_set('xdebug.max_nesting_level', 0);
set_time_limit(0);
error_reporting(E_ALL);
define("DEBUG", true);
$answers = "";
$count = 1;
$howmany = 100;
$time_start = microtime(true);
?>
<style>
div {
font-family: Segoe UI Light
}
.n {
font-size: 25px
}
.s{
font-size: 70px
}
</style><div class='n'>
<?php
function generate(){
global $howmany;
global $count;
$answer = [];
// 不要這樣做,這樣會沒辦法直接意識howmany有沒有底線的差異
//$howmany_ = $howmany + 1;
// 用迴圈會比用遞迴少上一些記憶體
while($count <= $howmany){
$operate = rand(0, 1); // 先決定OP,這樣可以控制第二個數字的範圍
$number_one = rand(40, 80);
if($operate == 1){
$rand_two = $number_one - 5;
$number_diff = rand(20, $rand_two); // rand(20, 35) ~ rand(20, 75) <<== Answer desu
$number_two = $number_one - $number_diff;
$answer[] = ['operation' => '-', 'number' => [$number_one,$number_two,$number_diff]];
}else{
$number_ans = rand($number_one, 99);
$number_two = $number_ans - $number_one;
$answer[] = ['operation' => '-', 'number' => [$number_one,$number_two,$number_ans]];
}
$count++;
}
return $answer;
}
$answer = generate();
foreach($answer as $ln => $line){
?>
<?=$ln?>. &nbsp;&nbsp;&nbsp;<?=$line['number'][0].' '.$line['operation'].' '.$line['number'][1]?> = <br/>
<?php
}
// 一堆BR的原因是因為湊一整頁,把答案印在新的紙上(100題剛好)
?>
<!-- 這邊說不定用CSS解決會比較好? -->
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</div>
<div class='s'>Answers</div>
<div class='n'>
<?php
foreach($answer as $ln => $line){
?>
<?=$ln.'. &nbsp;&nbsp;&nbsp;'. $line['number'][0].' '.$line['operation'].' '.$line['number'][1].' = '.$line['number'][2]?><br/>
<?php
}
?>
</div>
<?php
if( DEBUG ){
echo "<br />";
$time_end = microtime(true);
$time = $time_end - $time_start;
echo $time;
}
@PichuChen
Copy link
Author

我還在試著讀懂L47想做什麼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment