简体   繁体   中英

Inputting Multiple values through Scanf - C

I am trying to solve an SPOJ problem. I am stuck here.

For the input, it asks me the following to take as input

Next line contain n elements, ai (1<=i<= n) separated by spaces.

I can use a loop and input each element given separately by the user through scanf. But as per the problem criteria, I am assuming that we need to take the input through scanf at once in a single line. Like scanf("%d %d %d", &a1 &a2 etc).

But the range is like over 10^6, I am not sure how we can dynamically input multiple values through scanf in a single line.

You can run your iteration as you say, because scanf does not care what kind of whitespace separates integer inputs.

So: for (i = 0; i < n; ++i) scanf("%d", &array[i]); will work for inputs of the type:

3 2 1 2 3 8

as well as the type

3
2
1
2
3
8

Does not matter whether you input the numbers in a single line, this will work as scanf ignores the white spaces

int arr[1000001];  // Take an array to store the inputs

for(i=1;i<=n;i++)
{
    scanf("%d",&arr[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM