首頁 >後端開發 >php教程 >PHP练习(一)

PHP练习(一)

WBOY
WBOY原創
2016-06-13 12:29:201123瀏覽

PHP练习题(一)

程序1 .
题目: 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10% ; 利润高于10 万元, 低于20 万元时, 低于10万元的部分按10% 提成,高于 10万元的部分,可提成7.5%;20万到40 万之间时,高于20 万元的部分,可提成5%; 高于40 万元的部分, 可提成1%,从键盘输入当月利润I,求应发放奖金总数?(利用if else if)

<span style="color: #008080;"> 1</span> <span style="color: #800080;">$I</span> = 4324128<span style="color: #000000;">;</span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$bonus</span> = 0;  <span style="color: #008000;">//</span><span style="color: #008000;"> 奖金数</span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$I</span> ) {<span style="color: #008080;"> 5</span>     <span style="color: #800080;">$bonus</span> = <span style="color: #800080;">$I</span> * 1/10<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;">$I</span> > 100000 && <span style="color: #800080;">$I</span> ) {<span style="color: #008080;"> 7</span>     <span style="color: #800080;">$bonus</span> = <span style="color: #800080;">$I</span> % 100000 * 7.5/100 + (<span style="color: #800080;">$I</span> - <span style="color: #800080;">$I</span> % 100000) * 1/10<span style="color: #000000;">;</span><span style="color: #008080;"> 8</span> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$I</span> >= 200000 && <span style="color: #800080;">$I</span> ) {<span style="color: #008080;"> 9</span>     <span style="color: #800080;">$bonus</span> = (<span style="color: #800080;">$I</span> - 200000) * 5/100 + (<span style="color: #800080;">$I</span> % 100000 * 7.5/100) + (<span style="color: #800080;">$I</span> - <span style="color: #800080;">$I</span> % 100000) * 1/10<span style="color: #000000;">;</span><span style="color: #008080;">10</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> {</span><span style="color: #008080;">11</span>     <span style="color: #800080;">$bonus</span> = (<span style="color: #800080;">$I</span> - 400000) * 1/100 + (<span style="color: #800080;">$I</span> - 200000) * 5/100 + (<span style="color: #800080;">$I</span> % 100000 * 7.5/100) + (<span style="color: #800080;">$I</span> - <span style="color: #800080;">$I</span> % 100000) * 1/10<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: #008080;">14</span> <span style="color: #0000ff;">echo</span> 'bonus :' . <span style="color: #800080;">$bonus</span>.'<br>';

 

程序2.
题目: 输入三个整数x, y, z, 找出最大的数;

<span style="color: #008080;">1</span> <span style="color: #800080;">$x</span> = 4<span style="color: #000000;">;</span><span style="color: #008080;">2</span> <span style="color: #800080;">$y</span> = 7<span style="color: #000000;">;</span><span style="color: #008080;">3</span> <span style="color: #800080;">$z</span> = 2<span style="color: #000000;">;</span><span style="color: #008080;">4</span> <span style="color: #008080;">5</span> <span style="color: #800080;">$max</span> = <span style="color: #800080;">$x</span>><span style="color: #800080;">$y</span> ? <span style="color: #800080;">$x</span> : <span style="color: #800080;">$y</span><span style="color: #000000;">;</span><span style="color: #008080;">6</span> <span style="color: #800080;">$max</span> = <span style="color: #800080;">$z</span>><span style="color: #800080;">$max</span> ? <span style="color: #800080;">$z</span> : <span style="color: #800080;">$max</span><span style="color: #000000;">;</span><span style="color: #008080;">7</span> <span style="color: #008080;">8</span> <span style="color: #0000ff;">echo</span> 'max number :' . <span style="color: #800080;">$max</span> .'<br>';

 

 

程序3.
题目: 打印出所有的“水仙花数”, 所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。

<span style="color: #008080;"> 1</span> <span style="color: #800080;">$j</span> = 0; <span style="color: #008000;">//</span><span style="color: #008000;"> 数的个位 </span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$k</span> = 0; <span style="color: #008000;">//</span><span style="color: #008000;"> 数的十位</span><span style="color: #008080;"> 3</span> <span style="color: #800080;">$l</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;">$i</span> = 100; <span style="color: #800080;">$i</span>$i++<span style="color: #000000;">){</span><span style="color: #008080;"> 5</span>     <span style="color: #800080;">$j</span> = <span style="color: #800080;">$i</span> % 10<span style="color: #000000;">;</span><span style="color: #008080;"> 6</span>     <span style="color: #800080;">$k</span> = (<span style="color: #800080;">$i</span> % 100 - <span style="color: #800080;">$j</span>) / 10<span style="color: #000000;">;</span><span style="color: #008080;"> 7</span>     <span style="color: #800080;">$l</span> = (<span style="color: #800080;">$i</span> - <span style="color: #800080;">$i</span> % 100) / 100<span style="color: #000000;">;</span><span style="color: #008080;"> 8</span>     <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$i</span> == (<span style="color: #800080;">$j</span>*<span style="color: #800080;">$j</span>*<span style="color: #800080;">$j</span> + <span style="color: #800080;">$k</span>*<span style="color: #800080;">$k</span>*<span style="color: #800080;">$k</span> + <span style="color: #800080;">$l</span>*<span style="color: #800080;">$l</span>*<span style="color: #800080;">$l</span><span style="color: #000000;">)) {</span><span style="color: #008080;"> 9</span>         <span style="color: #0000ff;">echo</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> }

 

 

程序4.
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。 以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少。 (采用逆向思维,从后往前推)

<span style="color: #008080;">1</span> <span style="color: #800080;">$sum</span> = 1<span style="color: #000000;">;</span><span style="color: #008080;">2</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;">3</span>     <span style="color: #800080;">$sum</span> = (<span style="color: #800080;">$sum</span> + 1) * 2<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> '桃子总数:' .<span style="color: #800080;">$sum</span>. '<br>';

 

 

程序5.
题目: 有一分数序列: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13...求出这个数列的前20项之和。(注意分子和分母的变化规律)

<span style="color: #008080;">1</span> <span style="color: #800080;">$sum2</span> = 0<span style="color: #000000;">;</span><span style="color: #008080;">2</span> <span style="color: #800080;">$a</span> = 2<span style="color: #000000;">;</span><span style="color: #008080;">3</span> <span style="color: #800080;">$b</span> = 1<span style="color: #000000;">;</span><span style="color: #008080;">4</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;">5</span>     <span style="color: #800080;">$sum2</span> = <span style="color: #800080;">$sum2</span> + <span style="color: #800080;">$a</span>/<span style="color: #800080;">$b</span><span style="color: #000000;">;</span><span style="color: #008080;">6</span>     <span style="color: #800080;">$b</span> = <span style="color: #800080;">$a</span><span style="color: #000000;">;</span><span style="color: #008080;">7</span>     <span style="color: #800080;">$a</span> += <span style="color: #800080;">$b</span><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: #0000ff;">echo</span> '前20项之和为:' .<span style="color: #800080;">$sum2</span>.'<br>';

 


 
 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn