UVALive 6269 Digital Clock --枚舉,類比

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   strong   for   

題意:說不清楚,自己看吧,太噁心。

這題真是SB了,當時看了一下以為亂搞就好了,於是開始動手拍,結果拍了好幾個小時都沒拍出來,而且越想越想不通,直接把自己繞進去了,到最後全隊指著我這道題決勝,結果gg了。

教訓:甭管什麼題,想清楚了再拍。

解法:枚舉時間從00:00~23:59,數字顯示用7位01串表示,

,每次檢查與給出的觀察序列是否能夠邏輯一致。

關鍵在check部分,這部分我都寫了注釋了,應該比較易懂了。

代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <string>#include <vector>using namespace std;#define N 100007string num[12] = {"1110111","0010010","1011101","1011011","0111010","1101011","1101111","1010010","1111111","1111011"};struct node{    int mi,sec;    node(int _mi,int _sec)    {        mi = _mi;        sec = _sec;    }    node(){}}p[56];int h1[5][8],h2[5][8],broken[5][8];vector<node> ans;int get(int *a,int n){    for(int i=0;i<7;i++)        a[i] = num[n][i] - ‘0‘;}void add(node &x,int val){    x.sec += val;    x.mi += x.sec/60;    x.mi%=24;    x.sec%=60;}void getH1(node ka)         //觀察的{    memset(h1,0,sizeof(h1));    get(h1[0],ka.mi/10);    get(h1[1],ka.mi%10);    get(h1[2],ka.sec/10);    get(h1[3],ka.sec%10);}void getH2(node ka)         //枚舉的{    memset(h2,0,sizeof(h2));    get(h2[0],ka.mi/10);    get(h2[1],ka.mi%10);    get(h2[2],ka.sec/10);    get(h2[3],ka.sec%10);}bool check(){    int i,j;    for(i=0;i<4;i++)    {        for(j=0;j<7;j++)        {            if(h1[i][j] == 0 && h2[i][j] == 1) //觀察到沒有,現在枚舉到有 -> 壞了            {                if(broken[i][j] == 0)  //還不知道壞沒壞                    broken[i][j] = -1;  //定義為壞了                else if(broken[i][j] == 1)  //與前面矛盾                    return false;            }            else if(h1[i][j] == 1 && h2[i][j] == 0) //觀察到有,枚舉的沒有,那麼這個枚舉的不行                return false;            else if(h1[i][j] == 1 && h2[i][j] == 1) //都有            {                if(broken[i][j] == 0)  //還未定義好壞                    broken[i][j] = 1;  //肯定是好的                else if(broken[i][j] == -1)  //前面說壞了,矛盾                    return false;            }        }    }    return true;}int main(){    int n,i,j;    while(scanf("%d",&n)!=EOF)    {        for(i=0;i<n;i++)            scanf("%d:%d",&p[i].mi,&p[i].sec);        ans.clear();        for(int H=0;H<=23;H++)        {            for(int M=0;M<=59;M++)            {                node tmp;                tmp.mi = H,tmp.sec = M;                int flag = 1;                memset(broken,0,sizeof(broken));                for(i=0;i<n;i++)                {                    node now = tmp;                    add(now,i);                    getH1(p[i]);                    getH2(now);                    if(!check())                    {                        flag = 0;                        break;                    }                }                if(flag)                    ans.push_back(node(H,M));            }        }        if(ans.size() == 0)            puts("none");        else        {            printf("%02d:%02d",ans[0].mi,ans[0].sec);            for(i=1;i<ans.size();i++)                printf(" %02d:%02d",ans[i].mi,ans[i].sec);            puts("");        }    }    return 0;}
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.