ProgrammingVery hard
Which tool gives the fastest I/O in C for up to 10^6 integers in a contest setting?
- A. Read the whole input with fread into a buffer and parse the digits manually
- B. printf inside the loop to echo each value, costing one call per number
- C. Reading with scanf("%s") into a 10-byte buffer, then converting with atoi
- D. Reading character by character with fgetc and storing into an int by string concatenation
Correct answer: A. Read the whole input with fread into a buffer and parse the digits manually
Buffered reads with manual parsing avoid most per-number library overhead. The other options add per-number costs or string building.
More C Programming questions
- Each of 10^5 insertions into a dynamic array takes an O(n) memmove to keep the array sorted. Which total cost is right?
- A program uses a 2D table of size 1000 x 1000 with an O(1) update per cell and 1000 passes. How many cell updates are done?
- The number of comparisons a merge of two sorted arrays of sizes 4 and 6 makes in the worst case is at most?
- struct P { int a[3]; }; struct P p = {{1, 2, 3}}; struct P q = &p; printf("%d", (q->a + 2)); What is printed?
- char words[] = {"ab", "cd", "ef"}; char pp = words + 1; printf("%s", pp + 1); What is printed?
- int a[] = {10, 20, 30}; int p = a; p++; printf("%d", p + p[1]); What is printed?