The Number Off of FFF
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3721 Accepted Submission(s): 1230
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 1004
Sample Output
Case #1: 3 Case #2: 3
Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
考虑第一个报错的可能,如1,3, 4输出为1;3,5,5输出为2;
#include<iostream> #include<cstdio> using namespace std; int s[100010]; int main() { int t,count=1; cin>>t; while(t--) { int n,ans=0; bool flag=true; cin>>n; for(int i=0;i<n;i++) scanf("%d",&s[i]); printf("Case #%d: ",count++); if((s[1]-s[0]!=1)&&(s[2]-s[0]!=2))flag=false; //如果第二个与第一个的差不等于1,并且第三个与第一个的差不等于2,则说明第一个就错了; //例如:1,3,4,5; //假如是3,5,5则不满足条件,此时flag仍为true; for(int i=1;i<n;i++) { if(flag&&(s[0]+i!=s[i]))//在第一个没错的前提下,找到后面出错的那一个 { ans=i; break; } } printf("%d\n",ans+1);//在这里数组下标是从0开始的,而题目的计数是从1开始的,所以输出结果要加1 } return 0; }
2013四川省赛L