一个经典的找数字位置正确并且找到正确数列中存在的数字而错误的序列存在但是不是正确位置的算法。
看似很难的算法,但是lrj却很简单解决。
1 #include<cstdio> 2 3 #define Max 1010 4 5 int a[Max],b[Max]; 6 7 int main() 8 { 9 // freopen("in.txt","r",stdin); 10 int T,F,n; 11 int ncase = 0; 12 while(scanf("%d",&n) != EOF){ 13 if(n == 0) 14 break; 15 printf("Game %d:\n",++ncase); 16 for(int i = 1;i <= n; i++) 17 scanf("%d",&a[i]); 18 while(1){ 19 for(int i = 1;i <= n; i++) 20 scanf("%d",&b[i]); 21 if(b[1] == 0) 22 break; 23 T = 0,F = 0; 24 for(int i = 1;i <= n; i++) 25 if(a[i] == b[i]) T++; 26 for(int i = 1;i <= 9; i++){ 27 int c1 = 0,c2 = 0; 28 for(int j = 1;j <= n; j++){ 29 if(a[j] == i) c1++; 30 if(b[j] == i) c2++; 31 } 32 if(c1 < c2) F += c1; //关键步骤 33 else F += c2; 34 } 35 printf(" (%d,%d)\n",T,F-T); 36 } 37 } 38 return 0; 39 }