Summary of common input problems under c/c + +

Posted by sloppstack on Sat, 25 Dec 2021 08:11:47 +0100

Summarize the common input formats and processing methods in the algorithm test to prevent the occurrence of obvious algorithms. However, due to the poor processing of input problems, the questions were not answered. I fell on this problem.

1, Similarities and differences of several commonly used input functions

1. scanf()

You need to include the header file stdio h.

The return value of the scanf function is the number of variables read in.

#include <stdio.h>

int main() {
	int a, b;
	int m = scanf("%d %d", &a, &b);
	printf("a = %d, b = %d, m = %d", a, b, m);
	return 0;
}

The input and output results are as follows.

Here, the scanf function specifies that two variables must be input before proceeding. If two variables are not input, the program will not continue to execute, regardless of whether you press enter or space, but wait for input. This shows that the scanf function does not determine carriage returns and spaces as an input.

When scanf encounters spaces, tab s and carriage returns, it will determine the end. For example, if only one string is required but two strings separated by spaces are actually entered, only the first string is read in.

#include <stdio.h>

int main() {
	char a[10];
	scanf("%s", a);
	printf("%s", a);
	return 0;
}

scanf can also be used to enter multiple strings, each separated by a space.

#include <stdio.h>

int main() {
	char a[10], b[10];
	scanf("%s %s", a, b);
	printf("%s\n%s", a, b);
}


As when inputting other types of data, scanf must read enough two inputs to continue running, and the space between the two inputs and carriage return are not counted.

2. cin >>

You need to include the header file iostream and namespace std.

#include <iostream>
using namespace std;

Other features are basically the same as scanf, but there are still some subtle differences. You can refer to this article article . Since scanf is much faster than cin in terms of time efficiency, I solemnly use scanf when writing algorithm problems, which often has unexpected effects.

3. Other common input functions in C + +

Supplement: no enter, no echo input mode
_ The getch() function is a non echo function. When the user presses a character, the function will be read automatically without pressing enter. Some C language command-line programs will use this function for small games, but this function is not a standard function. Pay attention to portability! You need to include the header file conio h.

2, Summary of common input and output

1. Inform in advance that there are n input blocks, and the next N lines are the contents of these n input blocks

Calculate the value of A+B
Input Description: integer n in each line, followed by N lines. Enter two integer numbers A and B in each line, separated by spaces, and each group of numbers occupies one line.
Output Description: each example outputs A+B as a separate line.
Input example:
 2
 1 1
 4 4
Output example:
 2
 8

This is the simplest case. There's nothing to say. Go directly to the code.
C syntax:

scanf("%d",&n) ;
for( i=0 ; i<n ; i++ ) {
  scanf("%d %d",&a, &b);
  printf("%d\n",a+b);
}

c + + syntax:

cin >> n;
for( i=0 ; i<n ; i++ ) {
  cin >> a >> b;
  cout << a+b << endl;
}

2. Input does not indicate how many input blocks there are, but ends with EOF

Calculate the value of A+B
Input Description: enter two integers A and B on each line, separated by spaces.
Output Description: each example outputs A+B as a separate line.
Input example:
 1 1
 2 2
Output example:
 2
 4

Supplement: what is EOF
EOF is the abbreviation of End Of File. It is a predefined constant with a value of - 1. This character usually exists at the end of the text to indicate the end of the data.
The file with EOF as the end of file flag must be a text file. Because in text files, data is stored in the form of ASCII code values of characters. The range of ASCII code value is 0 ~ 255, and - 1 cannot appear, so EOF can be used as the end of file flag.

C syntax:

while (scanf("%d %d", &a, &b) != EOF) {
  ...
}

I wrote such a program.

#include <stdio.h>

int main() {
	int a, b;
	while (scanf("%d %d", &a, &b) != EOF) {
		printf("%d", a + b);
	}
}

The results are as follows. If you input from the console, every two numbers you input and press enter will output a result. But the problem is that the program will know that the loop will continue, and it does not know when to judge that the input is over.

If I type this. If several groups of numbers are input at once without using enter, two inputs will be read back in sequence in each cycle, and then printed on the screen. But I still don't know when it is determined to end the input.

This is because the file terminator exists in the case of file input. If you input from the console without manually entering the file control character, it is naturally impossible to end the loop.

However, ACM competition platform usually inputs data from text, so it will use the file end character EOF as the end flag. Therefore, in such problems without limiting the end flag, it is OK to directly judge whether the file end character EOF is read.

c + + syntax:

while (cin >> a >> b) {
  ...
}

3. Line breaks need to be processed during input

I once fell on such a problem. Obviously, the algorithm is very simple, but it took more than half an hour to process the input. Here is an example.

Linked list merging problem
Input Description: first enter a non negative number n, indicating that there are n linked lists. Next, enter n rows. Each row has several numbers separated by spaces, indicating each linked list.
Output Description: outputs the merged linked list.

Use getchar() to judge whether the input character is a newline '\ n'. The code is as follows. It only deals with the input problem and does not realize the specific logic of the problem.

#include <stdio.h>
#include <vector>
using namespace std;

int main() {
	int N;
	scanf("%d", &N);
	vector<vector<int>> linklists;
	for (int i = 0; i < N; i++) {
		vector<int> llist;
		int num;
		do {
			scanf("%d", &num);
			llist.push_back(num);
		} while (getchar() != '\n');
		linklists.push_back(llist);
	}

	//Print the input above
	for (auto& a : linklists)
	{
		for (auto b : a)
			printf("%d ", b);
		printf("\n");
	}
	return 0;
}

This article is not easy to write. If it helps you, please praise it!!

Topics: C C++ Algorithm