ccf 201712-3 crontab

原题链接 http://118.190.20.162/view.page?gpid=T66 

85分 就不写思路了 求大神指教

#include
#include
#include
#include
#include
#include
using namespace std;
string monthX[13] = { "", "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" };
string weekX[7] = { "sun","mon","tue","wed","thu","fri","sat" };
int monthDay[13] = { 0,31,0,31,30,31,30,31,31,30,31,30,31 };
struct  node
{
	vectorminutes;
	vectorhour;
	vectordayOfmonth;
	vectormonth;
	vectordayOfweek;
	string command;
}corntab[25];

vector getTimeStar(int kind)
{
	vectorstar;
	switch (kind)
	{
	case 0:
		for (int i = 0; i < 60; i++)
			star.push_back(i);
		break;
	case 1:
		for (int i = 0; i < 24; i++)
			star.push_back(i);
		break;
	case 2:
		for (int i = 1; i < 32; i++)
			star.push_back(i);
		break;
	case 3:
		for (int i = 1; i < 13; i++)
			star.push_back(i);
		break;
	case 4:
		for (int i = 0; i < 7; i++)
			star.push_back(i);
		break;
	}
	return star;
}
int getNum(string s)
{
	if (s[0] >= '0'&&s[0] <= '9')
	{
		return atoi(s.c_str());
	}
	else
	{
		for (int i = 0; i < 13; i++)
		{
			if (s == monthX[i])return i;
		}
		for (int i = 0; i < 7; i++)
		{
			if (s == weekX[i])return i;
		}
		return 0;
	}
}
vector getTimeNotStar(string s)
{
	vectorstar;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] >= 'A'&&s[i] <= 'Z')s[i] = s[i] - 'A' + 'a';
	}
	string t;
	int t1 = -1, t2 = -1;
	int flag = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] != '-'&&s[i] != ',')
		{
			t += s[i];
		}
		else if (s[i] == '-')
		{
			t1 = getNum(t);
			flag = 1;
			t = string();
		}
		else if(s[i]==',')
		{
			t2 = getNum(t);
			if (flag == 1)
			{
				for (int j = t1; j <= t2; j++)
					star.push_back(j);
				flag = 0;
			}
			else star.push_back(t2);
			t = string();
		}
	}
	t2 = getNum(t);
	if (flag == 1)
	{
		for (int j = t1; j <= t2; j++)
			star.push_back(j);
		flag = 0;
	}
	else star.push_back(t2);
	return star;
}

//获取年是否是闰年
bool checkYear(int y)
{
	if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
		return true;
	return false;
}
//获取今天是星期几
int getWeekDay(int y, int m, int d)
{
	int sum = 0;
	for (int i = 1; i < y; i++)
	{
		if (checkYear(i))sum += 366;
		else sum += 365;
	}
	if (checkYear(y))monthDay[2] = 29;
	else monthDay[2] = 28;
	for (int i = 1; i < m; i++)
	{
		sum += monthDay[i];
	}
	sum += d;
	return sum % 7;
}

int main()
{
	int n;
	string s, t, temp;
	cin >> n >> s >> t;
	int sy, sm, sd, sh, sM, ty, tm, td, th, tM;
	sy = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0');
	ty = (t[0] - '0') * 1000 + (t[1] - '0') * 100 + (t[2] - '0') * 10 + (t[3] - '0');
	sm = (s[4] - '0') * 10 + (s[5] - '0');
	tm = (t[4] - '0') * 10 + (t[5] - '0');
	sd = (s[6] - '0') * 10 + (s[7] - '0');
	td = (t[6] - '0') * 10 + (t[7] - '0');
	sh = (s[8] - '0') * 10 + (s[9] - '0');
	th = (t[8] - '0') * 10 + (t[9] - '0');
	sM = (s[10] - '0') * 10 + (s[11] - '0');
	tM = (t[10] - '0') * 10 + (t[11] - '0');
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			cin >> temp;
			if (j == 0)
			{
				if (temp == "*")corntab[i].minutes = getTimeStar(j);
				else corntab[i].minutes = getTimeNotStar(temp);
			}
			else if (j == 1)
			{
				if (temp == "*")corntab[i].hour = getTimeStar(j);
				else corntab[i].hour = getTimeNotStar(temp);
			}
			else if (j == 2)
			{
				if (temp == "*")corntab[i].dayOfmonth = getTimeStar(j);
				else corntab[i].dayOfmonth = getTimeNotStar(temp);
			}
			else if (j == 3)
			{
				if (temp == "*")corntab[i].month = getTimeStar(j);
				else corntab[i].month = getTimeNotStar(temp);
			}
			else if (j == 4)
			{
				if (temp == "*")corntab[i].dayOfweek = getTimeStar(j);
				else corntab[i].dayOfweek = getTimeNotStar(temp);
			}
		}
		cin >> corntab[i].command;
	}
	int j = sm, k = sd, o = sh, h = sM;
	for (int i = sy; i <= ty; i++, j = 1)
	{
		if(checkYear(i))monthDay[2] = 29;
		else monthDay[2] = 28;
		for (j; j <= 12; j++, k = 1)
		{
			if (i == ty && j > tm)break;
			for (k; k <= monthDay[j]; k++, o = 0)
			{
				if (i == ty && j == tm && k > td)break;
				int day = getWeekDay(i, j, k);
				for (o; o <= 23; o++, h = 0)
				{
					if (i == ty && j == tm && k == td && o > th)break;
					for (h; h <= 59; h++)
					{
						if (i == ty && j == tm && k == td && o == th && h == tM)break;
						for (int l = 0; l < n; l++)
						{
							int flag = 0;
							for (int x = 0; x < corntab[l].dayOfweek.size(); x++)
							{
								if (day == corntab[l].dayOfweek[x])
								{
									flag++;break;
								}
							}
							//if (flag != 1)break;
							for (int x = 0; x < corntab[l].month.size(); x++)
							{
								if (j == corntab[l].month[x])
								{
									flag++;break;
								}
							}
							//if (flag != 2)break;
							for (int x = 0; x < corntab[l].dayOfmonth.size(); x++)
							{
								if (k == corntab[l].dayOfmonth[x])
								{
									flag++;break;
								}
							}
							//if (flag != 3)break;
							for (int x = 0; x < corntab[l].hour.size(); x++)
							{
								if (o == corntab[l].hour[x])
								{
									flag++; break;
								}
							}
							//if (flag != 4)break;
							for (int x = 0; x < corntab[l].minutes.size(); x++)
							{
								if (h == corntab[l].minutes[x])
								{
									flag++; break;
								}
							}
							//if (flag != 5)break;
							if (flag == 5)
							{
								printf("%04d%02d%02d%02d%02d %s\n", i, j, k, o, h, corntab[l].command.c_str());
							}
						}
					}
				}	
			}
		}
	}

	//system("pause");
	return 0;
}

 

你可能感兴趣的:(ccf)