编写代码生成所有数对,其平方和等于 100

编写代码生成所有数对,其平方和等于 100 即:- x^2 + y^2 = 100

<?php


function getPossibleSquare($limit)

{

    $numbers = [];

    $number = 0;

    while(pow($number, 2) < $limit) {

        array_push($numbers, $number);

        $number++;

    }

    return $numbers;

}


function generateCombinations($numbers, $equals)

{

    $combinations = [];

    foreach($numbers as $x) {

        foreach($numbers as $y) {

            if( (pow($x, 2) + pow($y, 2)) == $equals ) {

                array_push($combinations, [$x, $y]);

            }

        }

    }

    return $combinations;

}


$limit = 100;


$combinations = generateCombinations( getPossibleSquare($limit), $limit );


紫衣仙女
浏览 176回答 2
2回答

开满天机

C语言的程序是:int num , i , j , root;scanf ( "%d", &num );//Input the numberroot = sqrt( num ); //We don't need to check the square upto input number. Just upto&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Its square rootfor( i = 1 ; i < root ; i++ ){&nbsp; &nbsp; j = i;&nbsp; &nbsp; for( ; j < root ; j++ )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if( ( ( i*i ) + ( j*j ) ) == num ) //If square is equal to number&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( "%d %d\n",i,j );&nbsp; &nbsp; &nbsp; //Print the combination&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

守着星空守着你

此代码用于 INT 号:$limit => 100<?php&nbsp; &nbsp; $limit = 100;&nbsp;&nbsp;&nbsp; &nbsp; $max_pair_value = ceil(sqrt($limit));&nbsp; &nbsp; for($i = 0; $i<=$max_pair_value; $i++){&nbsp; &nbsp; &nbsp; &nbsp; for($j = $i; $j<=$max_pair_value; $j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $answer = pow($i,2) + pow($j, 2);&nbsp; //YOUR CONDITION&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($answer == $limit){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pair[] = [0=>$i, 1=>$j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; print_r($pair);&nbsp; //UNIQUE PAIR?>
打开App,查看更多内容
随时随地看视频慕课网APP