给定答案序列和猜测序列,统计有几个数字位置正确,有几个数字在两个序列都出现过但位置不对。
输入包含多组数据。每组第一行是序列长度n,第二行是答案序列,接下来若干行是猜测序列。猜测序列全0时该组结束。n=0时整个输入结束。
样例输入:
4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
0
样例输出:
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
解法:
use std::io;
fn main() {
let mut gamecnt = 0;
loop {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let l: usize = buf.trim().parse().unwrap();
if l == 0 {
break;
}
gamecnt += 1;
println!("Game {}:", gamecnt);
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let ans: Vec<i32> = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();
loop {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let guess: Vec<i32> = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();
if guess[0] == 0 {
break;
}
let mut cnt = 0;
for i in 0..ans.len() {
if ans[i] == guess[i] {
cnt += 1;
}
}
let mut bcnt = 0;
for i in 1..=9 {
let mut c1 = 0;
let mut c2 = 0;
for j in 0..ans.len() {
if ans[j] == i {
c1 += 1;
}
if guess[j] == i {
c2 += 1;
}
}
bcnt += c1.min(c2);
}
println!("({},{})", cnt, bcnt - cnt);
}
}
}