uva725_一道水題(最佳化到了29ms)

來源:互聯網
上載者:User

標籤:acm   uva   演算法競賽入門經典   

///////////////////////////////////////////////////////////////////////////////////////////////////////
tt2767
聲明:本文遵循以下協議自由轉載-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0
查看本文更新與討論請點擊:http://blog.csdn.net/tt2767
連結被刪請百度: CSDN tt2767
///////////////////////////////////////////////////////////////////////////////////////////////////////

可以用 n * fghij 去枚舉 abcde 判重即可

由於fghij 可能自身重複較多,可以先判掉,節省時間;

如果不寫成函數的形式還能更快一些,因為第二次判段中x,已經判斷過了。

書中的做法是把兩個數字化成字串去,排序後判斷用時
123ms

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>//////////////////////#include<iostream>#include<algorithm>#include<string>#include <iterator>#include<sstream>#include<functional>#include<numeric>///////////////////////#include<vector>#include<map>#include <stack>#include<queue>#include<set>#include <bitset>#include <list>using namespace std;#define lch(x)  ((x) << 1)#define rch(x) ((x)<<1|1)#define dad(x) ((x)>>1)#define lowbit(x) ((x)&(-x))typedef  long long int LL;const int INF = ~0U>>1;const double eps = 1e-6;const long double PI = acos(0.0) * 2.0;//const int N = 10 + ;const int MAX = 98765,MIN = 1234;bool check(int x , int l1,int y ,int l2);int main(){    //ios::sync_with_stdio(false);#ifdef ONLINE_JUDGE#else    freopen("in.txt", "r", stdin);    freopen("out3.txt", "w", stdout);#endif    int n;    int tot = 0;    while(scanf("%d",&n)==1&& n)    {        if(tot++)  puts("");        bool flag=1;        for(int  i = MIN ; i<= MAX ; i++)        {            int l1 = log10(i)+1;            if(!check(i,l1,-1,-1))   continue;            int j = i*n;            int l2 = log10(j)+1;            if(j>MAX)       break;            if(!check(i,l1,j,l2))   continue;            flag = 0;            printf("%05d / %05d = %d\n",j,i,n);        }        if(flag)            printf("There are no solutions for %d.\n", n);    }    return 0;}bool check(int x , int l1,int y ,int l2){    bool re[10];    memset(re,0,sizeof(re));    if(l1==4&&l2==4)    return 0;    if(l1==4 || l2==4)    re[0]=1;    while(x)    {        if(re[x%10])   return 0;        re[x%10] = 1;        x/=10;    }    if(y != -1)    while(y)    {        if(re[y%10])   return 0;        re[y%10] = 1;        y/=10;    }    return 1;}

著作權聲明:本文為博主原創文章,允許非商業性轉載,轉載必須注名作者(CSDN tt2767)與本部落格連結:http://blog.csdn.net/tt2767。

uva725_一道水題(最佳化到了29ms)

聯繫我們

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