+ 2
How to take multiple inputs in the same line? Number of inputs is not given.
include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int k,n; scanf(" %d %d", &n,&k); char tt[n+1]; scanf(" %s", &tt); int ids[k]; for(int l=0;l<k;l++) { scanf(" %d", &ids[l]); } int u=0; if((k>=1)&&(k<=n)&&(n<=1000)) {for(int i=0;i<n;i++) { if((int)tt[i]==0) { for(int y=0;y<k;y++) { if(i==ids[y]); u++; } } }} if(u==0) { printf("NO"); } else { printf("YES"); } return 0; }
11 Answers
+ 2
Are there any possible ways to get k inputs in the same line without going to new lines?
+ 1
since you're using a for loop, it wi be best if you give your inputs on a separate lines
+ 1
yep
+ 1
What about saving the string on a separate file and then use a loop with fscanf until EOF?
+ 1
I mean putting the input of the user in a string with fgets, copying it onto a file and then reading the file with fscanf until EOF, putting every integer in an array
+ 1
You may take the line as a String input and use StringTokenizer split it using the delimiter(s) of your choice.
Scanner i = new Scanner(System.in);
String total = s.nextLine();
StringTokenizer stok = new StringTokenizer(total, " "); //The double quotes contain all the delimiters.
int n = stok.countTokens();
String[] a = new String[n];
for (int i = 0; i < n; i++)
a[i] = stok.nextToken();
0
the user have to input the first scanf and a space for the second input, the second scanf on a newline and the third on a several newlines depending on the k input, i will say this will be complicating to use on SoloLearn, it will better of if you chip in all necessary inputs and allow the code just to run
0
If I am not using any loop I think there is no way to take k number of inputs in the same line while k depends on user's input.
0
Can't we do it in java?
0
Lea it should be user input.
0
Lea thank you.