CSP 201809-2 买菜 暴力求解

201809-2 买菜

开1000000大数组

全局变量:放在全局静态区

int array[1000000];

动态分配:放在堆上

int *array=new int[1000000];

MyAnswer

#include
#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	int *h = new int[1000000];
	int *k = new int[1000000];
	memset(h, 0, 1000000 * sizeof(int));
	memset(k, 0, 1000000 * sizeof(int));
	int a, b, c, d;
	for (int i = 0; i < n; i++)
	{
		cin >> a >> b;
		for (int j = a; j < b; j++)
			h[j] = 1;
	}
	for (int i = 0; i < n; i++)
	{
		cin >> c >> d;
		for (int j = c; j < d; j++)
			k[j] = 1;
	}
	int sum = 0;
	for (int i = 0; i < 1000000; i++)
	{
		if (h[i] == 1 && k[i] == 1)
			sum++;
	}
	cout << sum << endl;
	delete[]h;
	delete[]k;
	return 0;
}

tips:

VS中都过得了编译,Dev中过不了编译

你可能感兴趣的:(#,CSP第二题)