Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4607 Accepted Submission(s): 2674
1 //2016.9.1 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 6 using namespace std; 7 8 int n, a[105], b[105], c[105], book[105][12], dit[105][5], vis[12], dit2[5], ans; 9 //book[i]记录a[i]出现了哪些数字,dit[i]记录a[i]不同位上的数字,vis记录x出现了哪些数字,dit2记录x不同位上的数字 10 11 bool judge(int x) 12 { 13 int cntb, cntc, pos = 0; 14 memset(vis, 0, sizeof(vis)); 15 memset(dit2, 0, sizeof(dit2)); 16 while(x) 17 { 18 dit2[pos] = x%10; 19 vis[x%10]++; 20 x /= 10; 21 pos++; 22 } 23 for(int i = 0; i < n; i++) 24 { 25 cntb = cntc = 0; 26 for(int j = 0; j < 4; j++) 27 if(dit2[j] == dit[i][j])cntc++; 28 if(cntc != c[i])return false; 29 for(int j = 0; j < 10; j++) 30 if(vis[j] && book[i][j]) 31 cntb += min(vis[j], book[i][j]); 32 if(cntb != b[i])return false; 33 } 34 return true; 35 } 36 37 int main() 38 { 39 int cnt; 40 while(scanf("%d", &n)!=EOF && n) 41 { 42 cnt = 0; 43 memset(book, 0, sizeof(book)); 44 for(int i = 0; i < n; i++) 45 { 46 scanf("%d%d%d", &a[i], &b[i], &c[i]); 47 int tmp = a[i], count = 0; 48 while(tmp) 49 { 50 book[i][tmp%10]++; 51 dit[i][count++] = tmp%10; 52 tmp /= 10; 53 } 54 } 55 for(int i = 1000; i < 10000; i++)//暴力枚举所有四位数 56 if(judge(i)) 57 { 58 ans = i; 59 cnt++; 60 } 61 if(cnt==1)printf("%d\n", ans); 62 else printf("Not sure\n");//如果答案不唯一,则不确定 63 } 64 return 0; 65 }
原文:http://www.cnblogs.com/Penn000/p/5830319.html