HDU 4474&&POJ 1465 BFS&&餘數判重,hdu4474

來源:互聯網
上載者:User

HDU 4474&&POJ 1465 BFS&&餘數判重,hdu4474

兩道題只是輸入輸出格式略有不同

給出n,m,m個數

要求產生一個數x,是n的倍數,並且只由這M個數構成(或不能含有這M個數中的任意一個),求最小的x,沒有則輸出-1(0);


BFS找每一個滿足n的倍數的數,用餘數判重最佳化

對於給定兩個數A,B,如果A和B模N都相同,設為C,即:

A=x*N+C

B=y*N+C

那麼如果在A後面加上一個數字能夠被N整除,則在B後面加上這個數字也肯定可以被N整除

即:(A*10+X)%N==(B*10+X)%N

因此可以利用模N的結果進行判重,只保留相同餘數的最小數,於是採用BFS搜尋


HDU4474:

#include "stdio.h"#include "string.h"#include "queue"using namespace std;int hash[11],num[10010],pre[10010];int n;void pri(int k){    if (pre[k]!=-1) pri(pre[k]);    printf("%d",num[k]);}void bfs(){    queue<int>q;    int i,cur,next;    for (i=1;i<=9;i++)        if(hash[i]==0)        {            cur=i%n;            if (cur==0) { printf("%d\n",i); return ;}            num[cur]=i;            q.push(cur);        }    while (!q.empty())    {        cur=q.front();        q.pop();        for (i=0;i<=9;i++)            if(hash[i]==0)            {                next=(cur*10+i)%n;                if (num[next]==-1)                {                    q.push(next);                    num[next]=i;                    pre[next]=cur;                }                if (next==0) {pri(next); printf("\n"); return ;}            }    }    printf("-1\n");    return ;}int main(){    int m,x,Case=1;    while (scanf("%d",&n)!=EOF)    {        memset(hash,0,sizeof(hash));        memset(num,-1,sizeof(num));        memset(pre,-1,sizeof(pre));        scanf("%d",&m);        while (m--)        {            scanf("%d",&x);            hash[x]=1;        }        printf("Case %d: ",Case++);        if(n==0) { printf("-1\n"); continue;}        bfs();    }    return 0;}

POJ1465:

#include "stdio.h"#include "string.h"#include "queue"using namespace std;int hash[11],num[10010],pre[10010];int n;void pri(int k){    if (pre[k]!=-1) pri(pre[k]);    printf("%d",num[k]);}void bfs(){    queue<int>q;    int i,cur,next;    for (i=1;i<=9;i++)        if(hash[i]==1)        {            cur=i%n;            if (cur==0) { printf("%d\n",i); return ;}            num[cur]=i;            q.push(cur);        }    while (!q.empty())    {        cur=q.front();        q.pop();        for (i=0;i<=9;i++)            if(hash[i]==1)            {                next=(cur*10+i)%n;                if (num[next]==-1)                {                    q.push(next);                    num[next]=i;                    pre[next]=cur;                }                if (next==0) {pri(next); printf("\n"); return ;}            }    }    printf("0\n");    return ;}int main(){    int m,x,Case=1;    while (scanf("%d",&n)!=EOF)    {        memset(hash,0,sizeof(hash));        memset(num,-1,sizeof(num));        memset(pre,-1,sizeof(pre));        scanf("%d",&m);        while (m--)        {            scanf("%d",&x);            hash[x]=1;        }      //  printf("Case %d: ",Case++);        if(n==0) { printf("0\n"); continue;}        bfs();    }    return 0;}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.