[USACO] Prime Cryptarithm

來源:互聯網
上載者:User

滿懷期待的開啟這道題,以為肯定是貪心演算法求解,結果很水的一道題,一次AC。

窮舉搜尋,雖然複雜度看上去很恐怖n的5次方,但是n很小,根本不對執行時間構成很大的影響,測試資料最長用了0.022秒

 

/*<br />ID:<br />LANG: C<br />TASK: crypt1<br />*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include <ctype.h><br />int notInTable(int pro, int table[]) {<br />while(pro) {<br />if(table[pro % 10] == 0)<br />return 1;<br />pro /= 10;<br />}<br />return 0;<br />}<br />int main() {<br />FILE *fin = fopen("crypt1.in", "r");<br />FILE *fout = fopen("crypt1.out", "w");<br />int n, i, j, k, r, s;<br />int total = 0, num[9] = { 0 }, table[10] = { 0 };<br />int cand, pro1, pro2, pro;<br />fscanf(fin, "%d", &n);<br />for(i = 0; i < n; i++) {<br />fscanf(fin, "%d", &k);<br />table[k] = 1;<br />num[i] = k;<br />}<br />for(i = 0; i < n; i++)<br />for(j = 0; j < n; j++)<br />for(k = 0; k < n; k++) {<br />cand = num[i] * 100 + num[j] * 10 + num[k];<br />for(r = 0; r < n; r++) {<br />pro1 = num[r] * cand;<br />if(pro1 > 999 || notInTable(pro1, table))<br />continue;<br />for(s = 0; s < n; s++) {<br />pro2 = num[s] * cand;<br />if(pro2 > 999 || notInTable(pro2, table))<br />continue;<br />pro = pro2 * 10 + pro1;<br />if(pro > 9999 || notInTable(pro, table))<br />continue;<br />else {<br />//fprintf(fout, "%d %d %d/n%d %d/n", num[i], num[j], num[k], num[s], num[r]);<br />total++;<br />}<br />}<br />}<br />}<br />fprintf(fout, "%d/n", total);<br />exit(0);<br />}<br /> 

分析的解法也是枚舉,但是它的迴圈沒有最佳化,乘數從10到100,被乘數從100到1000,分別進行遍曆,如果輸入資料沒有某個數位話,這樣額外進行了很多不必要的函數調用,當然,資料量小,無所謂了。。。

隔了一周確實不給力,進展嚴重滯後,以後爭取每周最少做三道題!!!

 

聯繫我們

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