参考ACM之家
http://www.acmerblog.com/hdu-4727-the-number-off-of-fff-7772.html
问题描述
Problem Description
X soldiers from the famous “FFF army” is standing in a line, from left to right.
You, as the captain of FFF, decides to have a “number off”, that is, each soldier, from left to right, calls out a number. The first soldier should call “One”, each other soldier should call the number next to the number called
out by the soldier on his left side. If every soldier has done it right, they will call out the numbers from 1 to X, one by one, from left to right.
Now we have a continuous part from the original line. There are N soldiers in the part. So in another word, we have the soldiers whose id are between A and A+N-1 (1 <= A <= A+N-1 <= X). However, we don’t know the exactly value of A, but we are sure the soldiers
stands continuously in the original line, from left to right.
We are sure among those N soldiers, exactly one soldier has made a mistake. Your task is to find that soldier.
Input
The rst line has a number T (T <= 10) , indicating the number of test cases.
For each test case there are two lines. First line has the number N, and the second line has N numbers, as described above. (3 <= N <= 105)
It guaranteed that there is exactly one soldier who has made the mistake.
Output
For test case X, output in the form of “Case #X: L”, L here means the position of soldier among the N soldiers counted from left to right based on 1.Sample Input
2
3
1 2 4
3
1001 1002 1004Sample Output
Case #1: 3
Case #2: 3
Java代码实现
package acmblog;
import java.util.Scanner;
public class FineWrongNum {
static int maxLen = (int) (1e5+9);
public static void main(String [] args)
{
int T=0;
int caseIndex=1;
Scanner scanner = new Scanner(System.in);
T = scanner.nextInt();
int N=0,Num[]=new int[maxLen];
//开始循坏处理测试用例
while(T-->0)
{
//输入N
N = scanner.nextInt();
//输入N个数据
for(int i =0; i<N; i++)
{
Num[i] = scanner.nextInt();
}
//寻找报数相差为1的士兵
//默认为第一个士兵报数出错
int flag =1;
for(int i=1;i<N;i++)
{
if(Num[i]-Num[i-1]!=1)
{
flag = i+1;
}
}
System.out.println("Case #"+caseIndex+": "+flag);
caseIndex++;
}
}
}