> php教程 > php手册 > 본문

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

WBOY
풀어 주다: 2016-05-20 11:51:49
원래의
1405명이 탐색했습니다.

程序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>';
로그인 후 복사

 


 
 

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿