PHP Logic Training Small Topic one (with my answer)

Source: Internet
Author: User
Tags gcd ord

"Program 1"
Topic: Classical Question: There are a pair of rabbits, from the 3rd month after the birth of a pair of rabbits each month, the rabbit grew up to the third month after each month again gave birth to a
To the rabbit, if the rabbit does not die, ask the number of rabbits each month for how much.
1. Procedure analysis: Rabbit's rule for series 1,1,2,3,5,8,13,21 ....

Fibonacci Series

<?php
$k =2; $k 1=0; $k 2=1; $sum =0;
for ($i =1; $i < $k; $i + +) {
$sum = $k 1+ 2;
$k 1= $k 2;
$k 2= $sum;

}
echo $k 2;
? >



"Program 2"
Title: Determine how many primes there are between 101-200, and output all primes.
1. Procedure analysis: The method of determining prime numbers: To remove 2 to sqrt (this number) separately, if divisible by a number,
It indicates that the number is not prime, whereas it is prime.

<?php
$sum =0;
For ($i =101 $i <=200; $i + +) {for
      ($j =2; $j <=sqrt ($i); $j + +) {
	  if ($i% $j ==0) {continue 2;
	  }
	  } echo $i. ' <br/> ';
	  $sum + +;
}
echo $sum. " A "
?>



I did "program 3."
Title: Print out all the "Narcissus number", the so-called "Narcissus number" refers to a three-digit number, its members of the digital cubic and equal to the number itself. For example:
153 is a "narcissus number", because the 153=1 three times side +5 of three times +3 of the three times side.
1. Program Analysis: Use for loop control 100-999 number, each number decomposition out of single-digit, 10, hundred.

<?php
$a = array ();
   For ($i =0 $i <=9; $i + +)
   {for
    ($j =0; $j <=9; $j + +)
    {for
     ($m =0; $m <=9; $m + +)
     {
      if ($i * $i * $i + $j * $j * $j + $m * $m * $m = = 100* $i + 10* $j + $m)
      {
       $a [] = 100* $i + 10* $j + $m
      ;
   }}} Print_r ($a);



"Program 4"
Title: Decompose a positive integer into decomposition. For example: Enter 90, print out 90=2*3*3*5.
Program analysis: N to decompose decomposition, you should find a minimum prime number k, and then complete the following steps:
(1) If this prime number is equal to n, then the process of decomposing decomposition is finished and printed out.
(2) if n<>k, but n can be divided by K, you should print out the value of K, and n divided by the quotient of K, as a new positive integer you n, repeat the first step.
(3) If n cannot be divisible by K, then repeat the first step using k+1 as the value of K.


<?php
$num =320;
For ($i =2 $i <= $num $i + +) {

   if ($num% $i ==0) { 
      echo $i;
	  $num = $num/$i;
	  $i =1
   }

? >



"Program 5"
Topic: Use the nesting of the conditional operator to complete this problem: the students of the academic achievement >=90 score with a said, 60-89 points between the use of B said, 60 points below
is expressed in C.
1. Procedure analysis: (A&GT;B)? A:b This is a basic example of a conditional operator.

If (academic performance >=60)
{
Else if (academic achievement <=89)
{
student =b
}
else
{
student =a;
}
}




"Program 6"
Title: Enter two positive integers m and n, and ask for their gcd and LCM.
1. Procedure analysis: The use of rolling division.


<?php
$m =80;
$n =40;
function Getit ($j) {for
($k =2; $k <= $j/2; $k + +) {
   if ($j% $k ==0) {$arr []= $k;}
   }
   return $arr;
}

$ARRM =getit ($m);
$arrn =getit ($n);
For ($i =count ($ARRM)-1; $i >=0; $i-) {for

	($p =count ($ARRN)-1; $p >=0; $p-) {
    if ($arrm [$i]== $arrn [$p]) {echo ' gcd: '. $arrm [$i];break 2}
}} Echo ' LCM: '. $m * $n/$ARRM [$i];
>



"Program 7"
Title: Enter a line of characters to count the number of English letters, spaces, numbers, and other characters.
1. Program Analysis: Using the while statement, the condition is that the character entered is not ' \ n '.


<?php
$str = "He ll l!1234565 _-!";
$len =strlen ($STR); $k =0; $a =0; $b =0;
$c =0; $d =0; while ($k < $len) {
if ($str [$k]== ') {$a + +;} ElseIf (Is_numeric ($str [$k])) {$b + +;}
ElseIf (64<ord ($str [$k]) && 91>ord ($str [$k]) | | 96<ord ($STR [$k]) && 123>ord ($str [$k])) {$c ++;} else{$d + +;}
$k + +;
}
echo $a. $b. $c. $d;
Var_dump (64< $str [2]);
? >



I did "program 8."
Title: Find the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (a total of 5 numbers are added at this time),
Several numbers are added with keyboard control.
1. Procedure analysis: The key is to calculate the value of each item.

<?php
$k =0; $t = ';
For ($y =1 $y <=5; $y + +) {
	$g = ';
	for ($j =1; $j <= $y; $j + +) {
    
	$g = $g. ' 2 ';
	
	}
$k +=intval ($g);
}
echo $k;
? >



"Program 9"
Title: If a number is equal to the sum of its factors, this number is called the "finished number". For example, 6=1+2+3. Programming to find out all that is within 1000.
Number.

<?php
//$num =320;
/* for
($k =2 $k <=1000; $k + +) {
if Array_sum (Part_prime ($k)) +1== $k) {echo $k. " ";}
}

function Part_prime ($num) {for
	($i =2; $i <= $num; $i + +) {
	   if ($num% $i ==0) { 
		  $arr []= $i;
		  $num = $num/$i;
		  $i =1;
	   }
	}
	return $arr;
}

* *
//var_dump (Part_prime ($num));

For ($i =2 $i <=1000; $i + +) {
   $sum =0;
   For ($k =2 $k <= $i/2; $k + +) {
   if ($i% $k ==0) {$sum + = $k;}
   }
   if ($sum +1== $i) {echo $i. ';}
}
? >



"Program 10"
Topic: A ball from 100 meters height free to fall, every time after landing back to the original height of half, and then fell, begging it in the 10th time, the total
Less rice. How high the 10th rebound.


<?php
$k =100;
$sum =100;
For ($i =1 $i <=10; $i + +) {
$k/=2;
$sum + + $k;
}
echo $sum. ' '. $k;
? >


"Program 11"
Question: There are 1, 2, 3, 4 digits, which can be composed of a number of different and no duplicate number of three digits. is how much.
1. Procedure analysis: Can fill in the hundred, 10 digits, single-digit digits are 1, 2, 3, 4. Make up all the permutations and then remove the arrangement that does not satisfy the condition.

C Language:

#include <stdio.h>
Main ()
{
      int i, j, K;
      int m=0;
      for (i=1;i<5;i++) for (
      j=1;j<5;j++) for (
      k=1;k<5;k++)
      {
               if (i!=j&&k!=j&&i !K)
                     m++;
                     printf ("%d%d%d\n", q,j,k);
       }
       printf ("%d\n", m);
}




"Program 12"
Title: The bonuses awarded by the Enterprise are based on the profit percentage. Profit (I) is less than or equal to $100,000, the bonus may be 10%; profit above 100,000 yuan, less than 200,000
Yuan, less than 100,000 yuan part by 10% commission, higher than 100,000 yuan, Cocoa Commission 7.5%, 200,000 to 400,000, higher than 200,000 yuan of the department
Points, can be a commission of 5%, 400,000 to 600,000 when the portion of more than 400,000 yuan, can be a commission of 3%, 600,000 to 1 million when, higher than 600,000 yuan of the portion, can
Commission 1.5%, more than 1 million yuan, more than 1 million yuan in the portion of 1% commission, from the keyboard input month profit I, the total number of bonuses should be issued.
1. Procedure analysis: Please use the axis to demarcation, positioning. Note that the definition needs to be defined as a growth integral type.

<?php
$profit =1060000;
$num =floor ($profit/100000);
Switch ($num) {case
    0:
      $bonus = $profit * 0.1;
	  break;
	Case 1:
	  $bonus = 100000* 0.1+ ($profit -100000) *0.075;
      break;
	Case 2: Case
	3:
	  $bonus = 100000* 0.1+100000*0.075+ ($profit -200000) *0.05;
	  break;
	Case 4: Case
	5:
	  $bonus = 100000* 0.1+100000*0.075+200000*0.05+ ($profit -400000) *0.03;
	  break;
	Case 6: Case
	7: Case
	8: Case
	9:
	  $bonus = 100000* 0.1+100000*0.075+200000*0.05+200000*0.03+ ($ profit-600000) *0.015;
	  break;
	Case:
	  $bonus = 100000* 0.1+100000*0.075+200000*0.05+200000*0.03+400000*0.015+ ($profit -1000000) *0.01;
	  break;
echo $bonus;
? >




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.