[] Data structure and algorithm study notes - "algorithm notes" -5

cin / cout

  • Use cin / cout need to add the header file and #include using namespace std;
  • Read multiple variables simultaneously:cin>>n>>db>>c>>str;
  • Read an entire row (getline function):
char str[100];
cin.getline(str, 100)
  • If the container is a string, the input required in the following manner:
	string str;
	getline(cin, str);
  • For cout, the wrap two ways: "\ n" or use endl;
  • If you want to control the accuracy of the double type, you need to add #include<iomanip>the header file
    output example:cout << setiosflags(ios::fixed) << setprecision << 123.4567 << endl;

Compare floating point

  • Floating-point number stored in the computer are not always accurate, it is necessary to introduce a minimum number of eps such an error can be corrected;
	const double eps = 1e-8;
  • Equality operator
    #define Equ(a,b) (((fabs)((a)-(b)))<(eps))
    Eg
#include <cstdio>
#include <cstring>
#include<cmath>
const double eps = 1e-45;
#define Equ(a,b) (((fabs)((a)-(b)))<(eps))

int main() 
{ 
	double db = 1.23;
	for (int i = 1; i <= 20; i++)
	{
		if (Equ(db, 1.23))	printf("true");
		else	printf("false");
	}
	return 0;
}

If written db == 1.23 no problem in this simple case, if a calculation error is large, the loss of accuracy can not be ignored.

  • Greater than operator
#define More(a,b) (((a)-(b))>(eps))
  • Less than operator
#define Less(a,b) (((a)-(b))<(-eps))
  • Greater than or equal to operator
#define MoreEqu(a,b) (((a)-(b))>(-eps))
  • Less than or equal to operator
#define Less(a,b) (((a)-(b))<(eps))
  • Pi pi
const double Pi = acos(-1.0);

the complexity

  • Generally divided into time complexity, spatial complexity, the encoding complexity

  • Time complexity of O (n): number of times in which the need to perform the basic operations of the algorithm level
    O (n) and (2n) O are equivalent: consumption increases with time n linearly increase the size of
    a high level of power of overwrites the low level power of O (with 3N 2 + + n-2) = O (with 3N 2 ) = O (n- 2 )
    clearly O (with 3N 2 + + n-2) will tend to O (CN 2 ), referred to as C constant time complexity
    as well as various time complexity, such as look-half the time complexity is O (logn), represents the number of time complexity; constant complexity of O (1) time consuming algorithm is expressed not with the growth of the size and growth.
    O (. 1) <O (logN) <O (n-) <O (n- 2 )

  • Space complexity and time complexity using the same wording that indicates the maximum data space algorithm need to consume
    Eg for an algorithm, if they consume maximum data space is a two-dimensional array, then the space complexity of the algorithm is O (n- 2 ). Since space is generally enough to use, so common policy space for time, such as hashing;

  • Coding complexity is a qualitative concept, there is no quantitative criteria, lengthy huge amount of thought makes the code algorithm

Three common input methods

  • while ... EOF type
    subject to a termination condition is not given (input all the data have been exhausted)
    Scanf function returns the parameter value which is the number of successfully read,
    normal input console is generally not fail, read only reached the end of the file due to take when a file can not be read phenomenon, will produce a read failure, this time scanf function returns -1, and use EOF language C (ie end of file) to represent 1;
    therefore, when the topic did not specify how much data needs to be read in, you can use the return value of scanf EOF to determine whether the input is completed.
	while (scanf("%d", &n) != EOF)
	{
		...
	}
	//只要scanf的返回值不为EOF(文件中的数据没有读完),就反复读入n;
	//当读入失败时(达到文件末尾),结束while循环。

In addition, when the input data in the console, and will not trigger EOF state, if you want to manually trigger EOF in the console, press <Ctrl + Z> key combination, press <Enter> key while you can end up
if it is reading string, there are two ways scanf and gets

	while (scanf("%s", str) != EOF)
	{
		...
	}
	while (gets(str) != NULL)
	{
		...
	}
  • BREAK ... type while
    subject to stop when the input data satisfies a condition input
    of this writing, there are two types:
    one is within the judgment of the EOF while ..., exit condition is satisfied when the interrupt (BREAK) while loop current;
    another wording is more compact, the exit condition in the while statement determines placed, and allowed to scanf separated by commas, such as
while (scanf("%d%d", &a, &b), a || b)
{
	...
}
  • while (T-) type
    In this type, the subject will be given of the number of sets of test data before a corresponding number of input data given number of groups. Since the number of sets of given test data, it is necessary to store T with a variable. After reading T, T cycles performed, each cycle to solve a set of input and output of datawhile (T--) {...}

Three common types of output

  • Normal output
  • Are each extra outputs a blank line
  • A blank line (grid) between two sets of output data, there is no empty row (grid) behind the last set of data
  • In the multi-point test, each cycle should be reset about variables and arrays, or in the state comes next set of data variables and arrays is not the initial state; general use memset function to reset the array or fill function.

Exercise
A + B O I Exercise

Title Description
Your task is to calculate a + b. It is specially designed for beginners acm title. You must find other similar topics with the title of solving the problem, these problems are also provided specifically for beginners.

Input consists of a series of a and b pairs, separated by spaces. A pair of A and b per line.

Output For each pair of input and a b, you need to turn the output of a, b and.

As the input and a second pair b, and the output thereof should be in the second row.

#include <cstdio>
#include <cstring>
#include<cmath>

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

A + B O Workshop II

Title Description
Your task is to calculate a + b.

The first input line is an integer N, the N rows have a back and b, separated by spaces.

For each pair of outputs of the input and a b, you need to output a corresponding row, b and. As a second pair and b, and also the corresponding second output line.

#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include<cmath>

int main() 
{ 
	int a, b, N;
	scanf("%d", &N);
	while (N--)
	{
		scanf("%d %d", &a, &b);
		printf("%d\n", a + b);
	}
	return 0;
}

A + B O practice III

Title Description
Your task is to calculate a + b.

Each input line is a pair of a and b. Where there will be a pair of 0 and 0 marks the end of input, and to do this calculation.

For each pair of outputs of the input and a b, you need to output a corresponding row, b and. As a second pair and b, and they are also output in a second row.

	#include <cstdio>
	#include <cstring>
	#include<cmath>

	int main() 
	{ 
		int a, b;
		while (scanf("%d %d", &a, &b),a||b)
		{
			printf("%d\n", a + b);
		}
		return 0;
	}

A + B O practice IV

Title Description
Your task is to calculate the number of integers.

The first number of each input line N, the number behind Bank has N.

If N = 0, it represents the input end of the line and not calculated.

And output required for each row of data in the corresponding output line.

	#include <cstdio>
	#include <cstring>
	#include<cmath>

	int main() 
	{ 
		int N;
		while (scanf("%d", &N),N)
		{
			int a,sum=0;
			while (N--)
			{
				scanf("%d", &a);
				sum += a;
			}
			printf("%d\n", sum);
		}
		return 0;
	}

A + B O V practice

Title Description Your task is to calculate the number of integers.

The first input line is a positive number N, the N rows behind. The first number of each line is M, M represents the number of the rear bank as well.

And output required for each row of data in the corresponding output line.

	#include <cstdio>
	#include <cstring>
	#include<cmath>

	int main() 
	{ 
		int N,M;
		scanf("%d", &N);
		while (scanf("%d", &M),N--)
		{
			int a, sum = 0;
			while (M--)
			{
				scanf("%d", &a);
				sum += a;
			}
			printf("%d\n", sum);
			if (N == 0) break;
		}
		return 0;
	}

A + B O practice VI

Title Description
Your task is to calculate the number of integers.

The first number of each input line N, the number behind Bank has N.

And output required for each row of data in the corresponding output line.

#include <cstdio>
	#include <cstring>
	#include<cmath>

	int main() 
	{ 
		int N;
		while (scanf("%d", &N)!=EOF)
		{
			int a, sum = 0;
			while (N--)
			{
				scanf("%d", &a);
				sum += a;
			}
			printf("%d\n", sum);
		}
		return 0;
	}

A + B O Exercise VII

Title Description
Your task is to calculate the sum of two integers.
Comprising a plurality of input lines, each input two integers a and B, separated by a space.
Output For each input, an output and a and b, each line connected to the output of a blank line.

	#include <cstdio>
	#include <cstring>
	#include<cmath>

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

A + B O practice VIII

Title Description Your task is to calculate the number of integers. The first line input to an integer N, the following N lines of M to enter an integer, integers M and enter in the same row.

Output For each input, output, and between, the output of each output number M a blank line.

#include <cstdio>
#include <cstring>
#include<cmath>

int main()
{
	int N, M;
	scanf("%d", &N);
	while (scanf("%d", &M), N--)
	{
		int a, sum = 0;
		while (M--)
		{
			scanf("%d", &a);
			sum += a;
		}
		printf("%d\n\n", sum);
		if (N == 0) break;
	}
	return 0;
}
Published 43 original articles · won praise 4 · Views 1222

Guess you like

Origin blog.csdn.net/weixin_42176221/article/details/99753519