百练 / 2018大数据研究中心夏令营上机考试 E: What time is it?

题目来源:http://poj.org/problem?id=1676

E: What time is it?

总时间限制1000ms   内存限制65536kB

描述

An accutron shows time with four digits, from 0000 to 2359.Every digit is represented by 3*3 characters, including '|'s, '_'s and blanks.When the LCD screen works well, the digits look like the following:

 _     _  _     _  _  _  _  _ 
| |  | _| _||_||_ |_   ||_||_|
|_|  ||_  _|  | _||_|  ||_| _|



There are two accutrons at hand. One shows the accurate time, and the other is15 minutes late. For example, at 8:25am, the first accutron shows '0825', whilethe second shows '0810'.

Unfortunately, there is something wrong with the two LCD screens, namely someparts of the digits missed. Your task is to decide the accurate time, accordingto the fragmental digits showed on the two accutrons.

输入

The first line of the input is a single integer t (1 <= t<= 20), the number of test cases. Each case contains three lines, indicatingthe time on the accurate accutron and the time on the slow accutron, separatedby a blank column. (Please refer to the Sample Input.)

输出

For each input, print the accurate time with four digits if itcan be ensured, or otherwise the string 'Not Sure'.

样例输入

2
    _  _  _      _     _ 
  | _  _||       _   || 

  | _ |_   |   | _    |_|
    _  _  _   _  _     _ 
  ||_  _||       _|  ||  
  | _ |_   |   ||     |_|

样例输出

Not Sure
0825

来源

POJ Monthly--2004.06.27 鄂继明

--------------------------------------------------------------

思路

神级模拟题。思路很清晰,但代码量很大,测试数据很难写。

根据将火柴棒表达式按照7段数码管的二进制编码成整数,再枚举可能的完整字符,最后8重循环暴力判断。

两个注意点:

1. 00:00 – 23:45 = 25 min

2. gets(char*)一次读入一行字符串时,如果之前用了scanf,要清缓冲区,试了下fflush(stdin)不行,不知道为什么,只能在gets之前的那个scanf后面再加一个gets把那一行的空字符串读进来免得影响后面的gets

3. 高版本的VS里用freopen, scanf会报错,只能用freopen_s, scanf_s,在源文件最前面加

#define _CRT_SECURE_NO_WARNINGS

可以解决该错误(对,“_CRT_SECURE_NO_WARNINGS”就是错误提醒里的那串)

注意是“最前面”,要在#include那一串之前

4. 注意输入重定向freopen("文件名","r",stdin)的应用

5. 注意goto的应用(虽然貌似老师都很反对用goto委屈

-----------------------------------------------------

代码 

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;

int a[8] = {};
int b[8] = {};
vector<int> v[8];
const int DIGIT[10] = {(1<<7)-1-(1<<2), (1<<3)+(1<<6), 1+(1<<3)+(1<<2)+(1<<4)+(1<<5), 1+(1<<3)+(1<<2)+(1<<6)+(1<<5),
	(1<<1)+(1<<2)+(1<<3)+(1<<6), 1+(1<<1)+(1<<2)+(1<<6)+(1<<5), 1+(1<<1)+(1<<2)+(1<<4)+(1<<5)+(1<<6),
	1+(1<<3)+(1<<6), (1<<7)-1, (1<<7)-1-(1<<4)};
const int L[8] = {0,3,6,9,13,16,19,22};
const int M[8] = {1,4,7,10,14,17,20,23};
const int R[8] = {2,5,8,11,15,18,21,24};

int main()
{
#ifndef ONLINE_JUDGE
	freopen("E.txt","r",stdin);
#endif
	char l1[30],l2[30],l3[30];
	int t,i,j,k,i0,i1,i2,i3,i4,i5,i6,i7,h0,h1,m0,m1,cnt=0;
	int ans[4] = {};
	char ch;
	bool is_frag = true, has_cout = false;
	scanf("%d",&t); gets(l1);					// 加一个gets把t这行的空字符串读进去
	while (t--)
	{
		memset(a,0,sizeof(a));
		for (i=0; i<8; i++)
		{
			v[i].clear();
		}
		/* 输入字符以七段码二进制转化为整数 */
		gets(l1);
		for (i=0; i<8; i++)
		{
			if (l1[M[i]] == '_')
			{
				a[i] += (1<<0);
			}
		}
		gets(l2);
		for (i=0; i<8; i++)
		{
			if (l2[L[i]] == '|')
			{
				a[i] += (1<<1);
			}
			if (l2[M[i]] == '_')
			{
				a[i] += (1<<2);
			}
			if (l2[R[i]] == '|')
			{
				a[i] += (1<<3);
			}
		}
		gets(l3);
		for (i=0; i<8; i++)
		{
			if (l3[L[i]] == '|')
			{
				a[i] += (1<<4);
			}
			if (l3[M[i]] == '_')
			{
				a[i] += (1<<5);
			}
			if (l3[R[i]] == '|')
			{
				a[i] += (1<<6);
			}
		}
		/* 枚举可能的数字 */
		for (i=0; i<8; i++)
		{
			for (j=0; j<=9; j++)
			{
				is_frag = true;
				for (k=0; k<7; k++)
				{
					if ((a[i] & (1<<k)) && !(DIGIT[j] & (1<<k)))
					{
						is_frag = false;
						break;
					}
				}
				if (is_frag)
				{
					v[i].push_back(j);
				}
			}
			if (v[i].size()==0)
			{
				printf("Not Sure\n");
				has_cout = true;
				break;
			}
		}
		/* 枚举所有的组合逐一判断 */
		cnt = 0;
		if (!has_cout)
		{
			for (i0=0; i0<v[0].size(); i0++)
			{
				for (i1=0; i1<v[1].size(); i1++)
				{
					for (i2=0; i2<v[2].size(); i2++)
					{
						for (i3=0; i3<v[3].size(); i3++)
						{
							for (i4=0; i4<v[4].size(); i4++)
							{
								for (i5=0; i5<v[5].size(); i5++)
								{
									for (i6=0; i6<v[6].size(); i6++)
									{
										for (i7=0; i7<v[7].size(); i7++)
										{
											b[0] = v[0].at(i0);
											b[1] = v[1].at(i1);
											b[2] = v[2].at(i2);
											b[3] = v[3].at(i3);
											b[4] = v[4].at(i4);
											b[5] = v[5].at(i5);
											b[6] = v[6].at(i6);
											b[7] = v[7].at(i7);
											h0 = b[0]*10+b[1];
											m0 = b[2]*10+b[3];
											h1 = b[4]*10+b[5];
											m1 = b[6]*10+b[7];
											if (h0>=0 && h0<=23 && h1>=0 && h1<=23 && m0>=0 && m0<=59 && m1>=0 && m1<=59)
											{
												if ((h0==h1 && m0-m1==15) || (h0==h1+1 && m0+60-m1==15) || (h0==0 && h1==23 && m0+60-m1==15))
												{
													if (cnt==1)
													{
														cnt = 2;
														goto mark;
													}
													cnt = 1;
													ans[0] = b[0];
													ans[1] = b[1];
													ans[2] = b[2];
													ans[3] = b[3];
												}
											}
										}

									}

								}

							}

						}

					}

				}

			}
mark:;
			if (cnt==1)
			{
				printf("%d%d%d%d\n",ans[0],ans[1],ans[2],ans[3]);
			}
			else
			{
				printf("Not Sure\n");
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值