[英]Inputting Multiple values through Scanf - C
I am trying to solve an SPOJ problem. 我正在尝试解决SPOJ问题。 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. 下一行包含n个元素,ai(1 <= i <= n)用空格隔开。
I can use a loop and input each element given separately by the user through scanf. 我可以使用循环并通过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.
但是根据问题标准,我假设我们需要在一行中一次通过scanf接受输入。 Like scanf("%d %d %d", &a1 &a2 etc).
就像scanf(“%d%d%d”,&a1&a2等)。
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. 但是范围超过10 ^ 6,我不确定如何通过scanf在一行中动态输入多个值。
You can run your iteration as you say, because scanf
does not care what kind of whitespace separates integer inputs. 您可以按照您的说法运行迭代,因为
scanf
不在乎哪种空格分隔整数输入。
So: for (i = 0; i < n; ++i) scanf("%d", &array[i]);
因此:
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 不管您是否在一行中输入数字,这都将有效,因为scanf会忽略空格
int arr[1000001]; // Take an array to store the inputs
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.