您的位置:首页 > 其它

poj 1676-What time is it?

2014-03-26 13:24 369 查看
博士上课讲了这道题,但是给的代码真心让人看不懂,借鉴了一些其余自己补充了好多,虽然时间是0ms,但是memory用到了700k,博士的代码memory只有20k,长度也比我少一半,看来差距真心大。

此题就是暴力,从00:00到23:59有1440分钟,只需要每次都列举然后去比对就可以了,但是关于如何比对就是仁者见仁智者见智了。先对7条线进行编号,从上到下从左到右分别是0123456,对应各自的二进制位数,这样的话,0对应的就是013456,写成2进制就是1111011就是123,因为给的会有残缺,所以只要保证没有用到的没有就可以了,0对应的就是2那条线没有,就是4。

把每一个数字按照这样编码,再分别用&符号进行判断,如果a&b是0就说明是满足这个数字的条件的,仔细想想吧。

比如给出的是1,编码是72,也就是1001000,循环中给出的数字是0,0 没有的数字是第二个也就是0000100.这2个2进制数进行&运算答案是0。即满足条件。

具体方法就是这样,写代码方面可参考我的AC代码。如果可以有优化的地方请告诉我。我也纳闷为啥别人能写怎么短。。谢谢各位大牛

#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
int x[8],time[8];
void longer(char *s)//把每一个都扩充到25位,25=12+1+12
{
int len = strlen(s);
for(int i = len; i < 25; i++)
s[i] = ' ';
s[25] = '\0';
}

void sstime(int t)//转分钟成8个数字
{
int tt = t-15;
if(tt<0) tt+=1440;
time[0] = t/60/10;
time[1] = t/60%10;
time[2] = t%60/10;
time[3] = t%60%10;
time[4] = tt/60/10;
time[5] = tt/60%10;
time[6] = tt%60/10;
time[7] = tt%60%10;
}

int code(char *s1, char *s2, char *s3)//编码
{
int cod=0;
char s[10];
strcpy(s,s1);
strcat(s,s2);
strcat(s,s3);
if(s[1] != ' ')
cod |= (1<<0);
for(int i = 3; i < 9; i++)
{
if(s[i] != ' ')
{
cod |= (1<<(i-2));
// cout<<"s["<<i<<"]="<<s[i]<<endl;
// cout<<"cod="<<cod<<endl;
}
}
return cod;
}

int main()
{
// freopen("input.txt","r",stdin);
int num[10]= {4,55,66,18,49,24,8,54,0,16};//数字里没有部分的编码
int t;
scanf("%d\n",&t);//要清除那个回车,因为下面有gets会读缓存区里的内容,不清除,S1就是回车了
while(t--)
{
char s1[30],s2[30],s3[30];
gets(s1);
gets(s2);
gets(s3);
if(strlen(s1) < 25)
longer(s1);
if(strlen(s2) < 25)
longer(s2);
if(strlen(s3) < 25)
longer(s3);
int i,j,flag = 0;
for(i = 0; i < 8; i++)
{
if(i >= 4)
flag = 1;
char temp1[4],temp2[4],temp3[4];
strncpy(temp1,s1+i*3+flag,3);
temp1[3] = '\0';
strncpy(temp2,s2+i*3+flag,3);
temp2[3] = '\0';
strncpy(temp3,s3+i*3+flag,3);
temp3[3] = '\0';
x[i] = code(temp1,temp2,temp3);
}
int times,ff;
for(ff = 0,i = 0; i <= 1440 && ff < 2; i++)//暴力
{
bool flag2 = true;
sstime(i);
for(j = 0; j < 8; j++)
{
if((num[time[j]] & x[j]))
{
flag2 = false;
break;
}
}
if(flag2)
{
ff++;
times = i;//保存这个时间
}
}
if(ff == 1)
{
sstime(times);
cout<<time[0]<<time[1]<<time[2]<<time[3]<<endl;
}
else
cout<<"Not Sure"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: