首頁 > php教程 > php手册 > PHP练习题(二) - 帅的打破吉尼斯

PHP练习题(二) - 帅的打破吉尼斯

WBOY
發布: 2016-05-20 11:51:48
原創
972 人瀏覽過

程序6.
题目: 假设某人有100,000现金,每经过一次路口需要进行一次交费交费规则为当他现金大于50,000时每次需要交%5,如果现金小于等于50,000时每次交5,000.问此人可以经过多少次这个路口。

<span style="color: #008080;"> 1</span> <span style="color: #800080;">$money</span> = 100000<span style="color: #000000;">;
</span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$i</span> = 0<span style="color: #000000;">;
</span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">while</span>(<span style="color: #800080;">$money</span> >= 5000<span style="color: #000000;">) {
</span><span style="color: #008080;"> 4</span>     <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$money</span> > 50000<span style="color: #000000;">) {
</span><span style="color: #008080;"> 5</span>         <span style="color: #800080;">$money</span> *= 95/100<span style="color: #000000;">;
</span><span style="color: #008080;"> 6</span>     } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$money</span> ) {
<span style="color: #008080;"> 7</span>         <span style="color: #800080;">$money</span> -= 5000<span style="color: #000000;">;
</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 9</span>     <span style="color: #800080;">$i</span>++<span style="color: #000000;">;
</span><span style="color: #008080;">10</span> <span style="color: #000000;">}
</span><span style="color: #008080;">11</span> <span style="color: #0000ff;">echo</span> '可以经过' . <span style="color: #800080;">$i</span> . '次路口<br>';
登入後複製

 

 

程序7.
题目: 打印九九乘法表

<span style="color: #008080;">1</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span> = 1; <span style="color: #800080;">$i</span> $i++<span style="color: #000000;">) {
</span><span style="color: #008080;">2</span>     <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span> = 1; <span style="color: #800080;">$j</span> $i; <span style="color: #800080;">$j</span>++<span style="color: #000000;">) {
</span><span style="color: #008080;">3</span>         <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$j</span> . 'x' . <span style="color: #800080;">$i</span> . '=' . <span style="color: #800080;">$i</span>*<span style="color: #800080;">$j</span> . '  '<span style="color: #000000;">;
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">5</span>     <span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;
</span><span style="color: #008080;">6</span> }
登入後複製

 

 

程序8.
题目: 百钱百鸡, 公鸡5文钱一只,母鸡3文钱一只,小鸡1文钱三只,用100文钱买100只, 问,公鸡,母鸡,小鸡各几只?

<span style="color: #008080;"> 1</span> <span style="color: #800080;">$x1</span> = 0; <span style="color: #008000;">//</span><span style="color: #008000;">公鸡个数</span>
<span style="color: #008080;"> 2</span> <span style="color: #800080;">$x2</span> = 0; <span style="color: #008000;">//</span><span style="color: #008000;">母鸡个数</span>
<span style="color: #008080;"> 3</span> <span style="color: #800080;">$x3</span> = 0; <span style="color: #008000;">//</span><span style="color: #008000;">小鸡个数</span>
<span style="color: #008080;"> 4</span> <span style="color: #0000ff;">for</span>( <span style="color: #800080;">$x1</span> = 1; <span style="color: #800080;">$x1</span> $x1++<span style="color: #000000;">) {
</span><span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">for</span>( <span style="color: #800080;">$x2</span> = 1; <span style="color: #800080;">$x2</span> $x1); <span style="color: #800080;">$x2</span>++<span style="color: #000000;">) {
</span><span style="color: #008080;"> 6</span>         <span style="color: #800080;">$x3</span> = 100 - <span style="color: #800080;">$x1</span> - <span style="color: #800080;">$x2</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 7</span>         <span style="color: #0000ff;">if</span>( 100 == (<span style="color: #800080;">$x1</span>*5 + <span style="color: #800080;">$x2</span>*3 + <span style="color: #800080;">$x3</span>/3<span style="color: #000000;">) ) {
</span><span style="color: #008080;"> 8</span>             <span style="color: #0000ff;">echo</span> '公鸡个数为:' . <span style="color: #800080;">$x1</span> . '母鸡个数为:' . <span style="color: #800080;">$x2</span> . '小鸡个数为' . <span style="color: #800080;">$x3</span> . '<br>'<span style="color: #000000;">;
</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">10</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">11</span> }
登入後複製

 

程序9.
题目: 求1到100内的素数

<span style="color: #008080;"> 1</span> <span style="color: #800080;">$num</span> = 2<span style="color: #000000;">;
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">while</span>(<span style="color: #800080;">$num</span> ) {
<span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$num</span> ) {
<span style="color: #008080;"> 4</span>         <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$num</span> . '  '<span style="color: #000000;">;
</span><span style="color: #008080;"> 5</span>     } <span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;"> 6</span>         <span style="color: #0000ff;">for</span>( <span style="color: #800080;">$j</span> = 2; <span style="color: #800080;">$j</span> $num; <span style="color: #800080;">$j</span>++<span style="color: #000000;">) {
</span><span style="color: #008080;"> 7</span>             <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$j</span> == <span style="color: #800080;">$num</span><span style="color: #000000;"> ) {
</span><span style="color: #008080;"> 8</span>                 <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$num</span> . '  '<span style="color: #000000;">;
</span><span style="color: #008080;"> 9</span>             } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>( <span style="color: #800080;">$num</span>%<span style="color: #800080;">$j</span> == 0<span style="color: #000000;">) {
</span><span style="color: #008080;">10</span>                 <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">11</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">12</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">13</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">14</span>     <span style="color: #800080;">$num</span>++<span style="color: #000000;">;
</span><span style="color: #008080;">15</span> }
登入後複製

 

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板