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

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

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

程序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으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿