使用Php解决Hackerearth挑战

时间:2017-03-30 09:30:38

标签: php

试图解决hackerearth的挑战

https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/find-product/

以下是迄今为止我最成功的两次尝试。

<html>

<head>

</head>

<body>

<?php


        $answer = 1;

        fscanf(STDIN, "%d\n %d\n %d\n %d\n %d\n", $n1, $n2, $n3, $n4, $n5);

        $answer = ($answer * 1) % 1000000007;
        $answer = ($answer * 2) % 1000000007;
        $answer = ($answer * 3) % 1000000007;
        $answer = ($answer * 4) % 1000000007;
        $answer = ($answer * 5) % 1000000007;
        echo $answer;


?> 

<?php

        $handle = fopen("C:\Apache24\htdocs\input.txt", "r");

        $answer = 1;

        while ($n = fscanf($handle, "%d %d %d %d %d")) {

            for ($i=0;  $i<5; $i++) {

                $answer = $answer * $n[$i];

            }

        }

        fclose($handle);

        echo $answer;


    ?>

</body>

</html>

hackerearth代码编辑器成功编译了我的代码,但在提交时说我没有通过测试。

我是php的新手。

请帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

这是hackerearth上接受的代码

$array = array (1, 2, 3, 4, 5);
$answer = 1;
foreach( $array as $value )
{
    $answer = ( $answer * $value ) % ( pow( 10, 9 ) + 7);
}
echo $answer;