John Davidson

php - Input from Kattis submission checker

0 comments
Message:


Learning PHP I choose to work on Neighborhood Watch. I believe that I have a working solution locally. I'm looking to get help simply importing the answers from the site. I did look at the help section which gave me this:


while (fscanf(STDIN, '%d%d', $number1, $number2) === 2) {}

But it gives no information on getting as many inputs as needed. In the example above, number1 and number2 will always be entered. I'm looking to take number2 and get more inputs equal to number2. Ive found similar questions in java and c++, but not PHP.



My Answer:
To read multiple inputs in PHP, you can use a loop to read the inputs based on the value of number2. Here's an example code snippet that demonstrates how you can achieve this:

php
// Read the first line of input
fscanf(STDIN, '%d', $number1);

// Loop through number2 inputs
for ($i = 0; $i < $number1; $i++) {
fscanf(STDIN, '%d', $input);

// Process the input as needed
// For example, you can store the inputs in an array
$inputs[] = $input;
}

// Now you have all the inputs stored in the $inputs array
// You can access them as needed
foreach ($inputs as $input) {
echo $input . "\n";
}


In this code snippet, we first read the value of number1 which indicates the number of inputs that will follow. Then, we use a loop to read number1 inputs and store them in an array called $inputs. Finally, we can access and process the inputs stored in the $inputs array.

I hope this helps you with importing the answers from the site for your Neighborhood Watch project. Let me know if you need further assistance!

Rate this post

3 of 5 based on 6601 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us