codeforces A. IQ test 例题
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is different in evenness.
The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains nspace-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness.
Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order.
5 2 4 7 8 10
3
本题也是很简单的题目。尤其是使用数组来解答就很多种方法可以做出来。
但是这里我不使用任何数组,直接查找出答案来,还是挺有意思的。
因为这样会要求仔细保存状态。
#include <iostream> using namespace std; void IQtestEVENNESS() { int n, a, even = 0, last = -1, lastNum = 0, curNum = -1, fix = -1; cin>>n; for (int i = 1; i <= n; i++) { cin>>a; even = a % 2; if (fix != -1 && fix != even) { cout<<i; return; } if (curNum != -1) { if (last == even) cout<<i - 2;//lastNum; else cout<<i - 1;//curNum; return; } else if (last == even) fix = even; else if (last != -1 && last != even) { curNum = a; } else lastNum = a; last = even; } cout<<false; }
这里保存了5个状态,要作对也不容易呢。