ACM input summary

Table of contents

1. Input structure

1. First-level input structure

2. Nested input structure

Second, the first-level input structure

HDU 1089 A+B for Input-Output Practice (I)

HDU 1090 A+B for Input-Output Practice (II)

HDU 1091 A+B for Input-Output Practice (III)

HDU 1095 A+B for Input-Output Practice (VII)

3. Nested input structure

HDU 1092 A+B for Input-Output Practice (IV)

HDU 1093 A+B for Input-Output Practice (V)

HDU 1094 A+B for Input-Output Practice (VI)

HDU 1096 A+B for Input-Output Practice (VIII)

Fourth, enter the string

(1) gets function

CodeForces 616A Comparing Two Long Integers

(2) Alternative function of gets

(3)scanf

HDU 5842 Lweb and String

(4) char array + cin

(4.1)cin

(4.2)cin.getline

(4.3)cin.get

(5)string

(5.1)cin

(5.2)getline

5. Enter other content

1. Enter integers and real numbers

CSU 1000 A+B (I)

CSU 1001 A+B (II)

CSU 1002 A+B (III)

2. Use strings to input integers and real numbers.

CSU 1340 A Sample Problem

else

3. Enter a combination of numbers and characters

4. Tabular data

5. Other input techniques

(1) Ignore input


1. Input structure

1. First-level input structure

Input structures are divided into 4 categories:

  • Enter only one use case
  • Enter use cases up to special use cases: HDU 1091
  • Enter use cases until end of file: HDU 1089
  • Enter an integer t first and then enter t use cases: HDU 1090

Each use case has a clear end condition.

2. Nested input structure

Each use case can contain several parts. Each part may include inputting only one use case, inputting use cases until special use cases, first inputting an integer t and then inputting t use cases, etc. Each use case can continue to be nested inside. . . .

Second, the first-level input structure

HDU 1089 A+B for Input-Output Practice (I)

topic:

Description

Your task is to Calculate a + b. 
Too easy?! Of course! I specially designed the problem for acm beginners. 
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim. 

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. 

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. 

Sample Input

1 5
10 20

Sample Output

6
30

Code:

<iostream>
using namespace std;

int main()
{
	int a, b;
	while (cin >> a >> b)cout << a + b << endl;
	return 0;
}

HDU 1090 A+B for Input-Output Practice (II)

topic:

Description

Your task is to Calculate a + b. 

Input

Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line. 

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. 

Sample Input

2
1 5
10 20

Sample Output

6
30

Code:

<iostream>
using namespace std;

int main()
{
	int n, a, b;
	cin >> n;
	while (n--)
	{
		cin >> a >> b;
		cout << a + b << endl;
	}
	return 0;
}

HDU 1091 A+B for Input-Output Practice (III)

topic:

Description

Your task is to Calculate a + b.

Input

Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed. 

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input. 

Sample Input

1 5
10 20
0 0

Sample Output

6
30

Code:

<iostream>
using namespace std;

int main()
{
	int a, b;
	while (cin >> a >> b)
	{
		if (a == 0 && b == 0)break;
		cout << a + b << endl;
	}
	return 0;
}

HDU 1095 A+B for Input-Output Practice (VII)

topic:

Description

Your task is to Calculate a + b. 

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line. 

Output

For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line. 

Sample Input

1 5
10 20

Sample Output

6

30

Code:

#include<iostream>
using namespace std;

int main()
{
	int a, b;
	while (cin >> a >> b)cout << a + b << endl << endl;
	return 0;
}

3. Nested input structure

HDU 1092 A+B for Input-Output Practice (IV)

topic:

Description

Your task is to Calculate the sum of some integers. 

Input

Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed. 

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input. 

Sample Input

4 1 2 3 4
5 1 2 3 4 5
0 

Sample Output

10
15

Code:

#include<iostream>
using namespace std;

int main()
{
	int n, a, sum;
	while (cin >>n)
	{
		if (n == 0)break;
		sum = 0;
		while (n--)
		{
			cin >> a;
			sum += a;
		}
		cout << sum << endl;
	}
	return 0;
}

HDU 1093 A+B for Input-Output Practice (V)

topic:

Description

Your task is to calculate the sum of some integers. 

Input

Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. 

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input. 

Sample Input

2
4 1 2 3 4
5 1 2 3 4 5

Sample Output

10
15

Code:

#include<iostream>
using namespace std;

int main()
{
	int t, n, a, sum;
	cin >> t;
	while (t--)
	{
		cin >> n;
		sum = 0;
		while (n--)
		{
			cin >> a;
			sum += a;
		}
		cout << sum << endl;
	}
	return 0;
}

HDU 1094 A+B for Input-Output Practice (VI)

topic:

Description

Your task is to calculate the sum of some integers. 

Input

Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line. 

Output

For each test case you should output the sum of N integers in one line, and with one line of output for each line in input. 

Sample Input

4 1 2 3 4
5 1 2 3 4 5

Sample Output

10
15

Code:

#include<iostream>
using namespace std;

int main()
{
	int n, a, sum;
	while (cin >> n)
	{
		sum = 0;
		while (n--)
		{
			cin >> a;
			sum += a;
		}
		cout << sum << endl;
	}
	return 0;
}

HDU 1096 A+B for Input-Output Practice (VIII)

topic:

Description

Your task is to calculate the sum of some integers. 

Input

Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line. 

Output

For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs. 

Sample Input

3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output

10

15

6

Code:

#include<iostream>
using namespace std;

int main()
{
	int t, n, a, sum;
	cin >> t;
	while (t--)
	{
		cin >> n;
		sum = 0;
		while (n--)
		{
			cin >> a;
			sum += a;
		}
		cout << sum << endl;
		if (t)cout << endl;
	}
	return 0;
}

Fourth, enter the string

(1) gets function

C language input strings are represented by char arrays.

The gets function reads the entire line of input until it encounters a newline character, then discards the newline character, stores other characters, and adds a '\0' at the end

CodeForces 616A Comparing Two Long Integers

topic:

Description

You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.

The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the functioninput() in Python2 instead of it use the function raw_input().

Input

The first line contains a non-negative integer a.

The second line contains a non-negative integer b.

The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.

Output

Print the symbol "<" if a < b and the symbol ">" if a > b. If the numbers are equal print the symbol "=".

Sample Input

Input

9
10

Output

<

Input

11
10

Output

>

Input

00012345
12345

Output

=

Input

0123
9

Output

>

Input

0123
111

Output

>

It just compares the size of two integers, it's very simple.

The main reason is that I am not familiar with C language.

Code:

#include<stdio.h>
#include<stdlib.h> 
#include<string.h>
 
 
int main()
{
	char*s1 = new char[1000000];
	char*s2 = new char[1000000];
	int l1, l2;
	char c;
	while (gets(s1) && gets(s2))
	{
		l1 = strlen(s1);
		l2 = strlen(s2);
		int i = 0, j = 0;
		while (s1[i] == '0'&& l1)
		{
			i++;
			l1--;
		}
		while (s2[j] == '0'&& l2)
		{
			j++;
			l2--;
		}
		if (l1 > l2)c='>';
		else if (l1 < l2)c='<';
		else
		{
			while (s1[i])
			{
				if (s1[i]>s2[j])
				{
					c = '>';
					break;
				}
				if (s1[i]<s2[j])
				{
					c = '<';
					break;
				}
				i++;
				j++;
				l1--;
			}
			if (l1 == 0)c = '=';
		}
		printf("%c\n", c);
	}
	return 0;
}

(2) Alternative function of gets

You can use fgets or gets_s instead of the gets function. The function functions are different but similar.

(3)scanf

scanf is the form of reading words. It ignores all whitespace characters at the beginning and ends when it reads any whitespace character.

HDU 5842 Lweb and String

topic:

This question is to select a mapping from a set of 26 letters to a set of integers, and then find the maximum length of the longest increasing subsequence of the sequence corresponding to the character array in each row.

If you think I'm talking about dynamic programming, you're stupid.

Because the mapping can be optional, the meaning of this question is to input a string (only lowercase letters) and output how many different lowercase letters it has.

Yes, that’s right, that’s it, gone.

Code:

#include<iostream>
#include<cstring>
using namespace std;

char c[100001];
int l[26];

int main()
{
    int t;
    scanf("%d", &t);
    for (int cas = 1; cas <= t; cas++)
    {
        scanf("%s", c);
        int len = strlen(c);
        for (int i = 0; i < 26; i++)l[i] = 0;
        for (int i = 0; i < len; i++)l[c[i] - 'a']++;
        int sum = 0;
        for (int i = 0; i < 26; i++)sum += (l[i]>0);
        printf("Case #%d: %d\n", cas, sum);
    }
    return 0;
}

(4) char array + cin

(4.1)cin

cin inputs a string represented by a char array and ends the input when it encounters a blank character.

Such as CSU 1260, CSU 1059, CSU 1019

(4.2)cin.getline

cin.getline can read the entire line of string, discarding the newline character after each line read.

Such as CSU 1029

(4.3)cin.get

cin.get is very similar to cin.getline, but newlines are not discarded but continue to be placed in the input buffer.

CSU 2050

(5)string

(5.1)cin

The cin input string ends with a blank character, such as CSU 1041, CSU 1112, CSU 1042 remote control robot

(5.2)getline

getline reads an entire line, such as POJ 1035

5. Enter other content

The input content can be simply classified into two types, numbers and strings. If classified carefully, there are many types.

1. Enter integers and real numbers

Inputting integers is the most common situation, and there are many examples, such as the HDU IO series above.

CSU 1000/1001/1002 A+B problems cover the input and output problems of ordinary integers, large integers, and real numbers.

CSU 1000 A+B (I)

Description

Your task is to Calculate a + b.

Input

Input contains multiple test cases. Each test case consists of a pair of integers a and b(0<=a,b<=20), separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 1

Sample Output

2

Code:

#include<iostream>
using namespace std;
 
int main()
{
    inta, b;
    while(cin >> a >> b)
    {
        cout << a + b << endl;
    }
    return 0;
}

CSU 1001 A+B (II)

Description

Your task is to Calculate a + b.

Input

There are multiple test cases. Each test case contains only one line. Each line consists of a pair of real number a and b(0<=a,b<=1000000), separated by a space.

Output

For each case, output the answer in one line rounded to 4 digits after the decimal point.

Sample Input

1 5

10 20

0.1 1.5

Sample Output

6.0000

30.0000

1.6000

Code:

#include<iostream>
using namespace std;
 
int main()
{
    double a, b;
    while(cin >> a >> b)
    {
        printf("%.4f",a+b);
        cout <<  endl;
    }
    return 0;
}

CSU 1002 A+B (III)

Description

Your task is to Calculate a + b.

Input

There are multiple test cases. Each test case contains only one line. Each line consists of a pair of integers a and b(1=< a,b <=1016), separated by a space. Input is followed by a single line with a = 0, b = 0, which should not be processed.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20
10000000000000000 10000000000000000
0 0

Sample Output

6
30
20000000000000000

Code:

#include<iostream>
using namespace std;
 
int main()
{
    long long a, b;
    while(cin >> a >> b)
    {
        if(a == 0 && b == 0)break;
        cout << a + b<<endl;
    }
    return 0;
}

2. Use strings to input integers and real numbers.

CSU 1340 A Sample Problem

topic:

Description

   My girlfriend loves 7 very much, she thinks it is lucky! If an integer contains one or more 7, she will think it is lucky too!

Input

    The first line has one integer T (1 <= T <= 100), means there are T test cases.
    For each test case, there is only one line with an integer X (1 <= X <= 109).

Output

    For each test case, if X contains one or more 7, output “Lucky” (without quotation marks), otherwise output “Unlucky” (without quotation marks).

Sample Input

4
7
11
568
17171

Sample Output

Lucky
Unlucky
Unlucky
Lucky

HINT

    We mainly use this topic to feel what a complete ACM competition topic should be like.

    The "format" of the ACM competition question itself is obvious:

    the first part is a description of the question, which explains the background of the question, what tasks you need to program to complete, etc.

    The second part is the input convention, which will tell you whether the question has multiple sets of test data, the format of each set of test data, the type of each parameter (integer, floating point number, string, etc.) and the value range, etc.

    The third part is the agreement on output, that is, what content your program should output and what the specific format is.

    The fourth part is some examples. These examples only show you a part of the test data and the corresponding answers. The purpose is to better help you understand the meaning of the question and the requirements for the output format.

    You may be full of questions like I was when I first came into contact with ACM. Here I will list a few common questions for you: <1>

    If the question has multiple sets of data, do we have to combine all the data like the example? The results are calculated first and then output together?

    This is not necessary. You can output the results corresponding to this set of data immediately after processing a set of data.

    But if you type these examples manually, you will find that input and output are mixed together on the screen, unlike the examples where input and output are separated. Will this not meet the requirements? Then please read <2>, I believe your questions will be answered.

    <2> How does OJ (Online Judge) know if my program is written correctly?

    OJ does not intelligently analyze whether your code is correct or not, but uses another ingenious idea to judge whether your program meets the requirements: "feed" some input data to your program (just like when you input data on the keyboard) (like hitting Enter after typing something), and then compare what your program "outputs" (just like what your program outputs as you see it on the screen) with the standard answer. If you If the answer obtained by the program is exactly the same as the standard answer, then you will pass the question.

    Therefore, it doesn't matter if the input and output on the screen are mixed together when you manually enter the example, as long as your program "produces" something exactly like the standard answer.

    It is precisely because of this judgment mechanism that you should not let the program print some prompt statements, such as "Please input two integers", etc., because everything output by your program will be compared with the standard answer. Once output If you answer a question like this without a statement requiring output, it will be very different from the standard answer, and you will naturally think that your program is incorrect.

    It is worth mentioning that although such a judgment mechanism does not seem to be able to perfectly know whether your program is right (think about why?), if the test data is strong enough, "your program is right" The probability will be very high.

    <3> Why did my program pass all the examples, but still failed to pass this question in the end?

    The sample of the question is only a small part of the test data. The purpose is to better help you understand the meaning of the question and the requirements for the output format. Therefore, passing the sample does not mean that all the test data can be passed. Only by passing all the test data can you get "Accept", which means you have passed the question.

    <4> After the answers to each set of test data are output, do they need to be line-wrapped?

    Yes, even if it is not explicitly stated in the question, we should output a "line feed" (that is, "\n") after outputting the answers to each set of test data, just as the example indicates.

    Next, let’s talk about the idea of ​​​​this topic. I believe everyone should have thought of the algorithm: just judge whether each digit of the integer read in is 7.

Code:

#include<iostream>
#include<cstring>
using namespace std;
 
char c[10];
 
int main()
{
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> c;
        int f = 0;
        for (int i = 0; i < strlen(c); i++)if (c[i] == '7')f = 1;
        if (f)cout << "Lucky" << endl;
        else cout << "Unlucky" << endl;
    }
    return 0;
}

The ACM question is like this. It is obvious that the input is an integer, but I can completely think that the input is each character. I only need to see whether each character is 7, without any mathematical calculation.

else

CSU 1003
CSU 1053
HDU 1717 Decimal Fractions 2

3. Enter a combination of numbers and characters

CodeForces 697B Varnish

4. Tabular data

SGU 454 Kakuro

5. Other input techniques

(1) Ignore input

Some input content is not needed, so you can omit the variables.

If you enter three integers msn, but s is not needed, then the code is:

int main()
{
	int m, n;
	cin >> m >> n >> n;
......

If the input that needs to be ignored is ranked last, even this kind of coverage is not needed, just do not input it directly. OJ does not require that all input data must be input into the program, OJ only verifies the output.

Guess you like

Origin blog.csdn.net/nameofcsdn/article/details/132224995