猜数字游戏的提示(UVa340)
生活随笔
收集整理的這篇文章主要介紹了
猜数字游戏的提示(UVa340)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目具體描述見:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=276
C++11代碼如下:
1 #include<iostream> 2 using namespace std; 3 #define maxn 1010 4 int main() { 5 int n; 6 int a[maxn], b[maxn]; 7 int count = 0; 8 while ((cin >> n) && n) { 9 cout << "Game " << ++count << ':' << endl; 10 for (int i = 0; i < n; i++) cin >> a[i]; 11 for (;;) { 12 int A = 0, B = 0; 13 for (int i = 0; i < n; i++) { 14 cin >> b[i]; 15 if (a[i] == b[i]) A++; 16 } 17 if (b[0] == 0) break; 18 for (int d = 1; d < 10; d++) { //統(tǒng)計(jì)每個數(shù)字在數(shù)組a、b中的出現(xiàn)次數(shù) 19 int c1 = 0, c2 = 0; 20 for (int i = 0; i < n; i++) { 21 if (a[i] == d) c1++; 22 if (b[i] == d) c2++; 23 } 24 (c1 < c2) ? (B += c1) : (B += c2); //取小者計(jì)入B,a、b中未同時出現(xiàn)的不會計(jì)入,位置正確的(A)也會計(jì)入 25 } 26 cout << " (" << A << ',' << B - A << ')' << endl; //B-A即為都出現(xiàn)且位置不對 27 } 28 } 29 return 0; 30 }轉(zhuǎn)載于:https://www.cnblogs.com/pgzhang/p/9224802.html
總結(jié)
以上是生活随笔為你收集整理的猜数字游戏的提示(UVa340)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android开发中EditText自动
- 下一篇: select标签中option的显示隐藏