gpt4 book ai didi

php - 将整数插入数组的问题

转载 作者:行者123 更新时间:2023-12-02 06:58:38 24 4
gpt4 key购买 nike

我一直在尝试解决 Project Euler 问题 1,但我觉得我缺少一些基本的东西。你能帮我修复我的错误吗?

我首先尝试了这个:

<?php
/*
** Project Euler Problem 1
** If we list all the natural numbers below 10 that are multiples of 3 or 5,
** we get 3, 5, 6 and 9. The sum of these multiples is 23.
** Find the sum of all the multiples of 3 or 5 below 1000.
*/
$numberOne = 3;
$numberTwo = 5;
$endingIndex = 1000;
$multiplesOfNumbers = array();

for ($index = 1; $index <= $endingIndex; $index++) {
if ($index % $numberOne == 0 && $index % $numberTwo == 0) {
$multipleOfNumbers[] = $index;
}
}
echo $multiplesOfNumbers;
?>

输出:

Array

所以我尝试用 array_push 代替,像这样:

<?php
/*
** Project Euler Problem 1
** If we list all the natural numbers below 10 that are multiples of 3 or 5,
** we get 3, 5, 6 and 9. The sum of these multiples is 23.
** Find the sum of all the multiples of 3 or 5 below 1000.
*/
$numberOne = 3;
$numberTwo = 5;
$endingIndex = 1000;
$multiplesOfNumbers = array();

for ($index = 1; $index <= $endingIndex; $index++) {
if ($index % $numberOne == 0 && $index % $numberTwo == 0) {
// $multipleOfNumbers[] = $index;
array_push($multiplesOfNumbers, $index);
}
}
echo $multiplesOfNumbers;
?>

输出是一样的。我错过了什么?

最佳答案

试试这个方法:

print_r($multiplesOfNumbers);

关于php - 将整数插入数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26047697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com