zoukankan      html  css  js  c++  java
  • HDUOJ----The Number Off of FFF

    The Number Off of FFF

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 169    Accepted Submission(s): 83

    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
     此题解法多样,关键是要理解其意思....
    题意为:
     从左向右依次报数,但是必定有一人报错,其中报错的意思是数据是任意的...比如 1 2 3 4 4 5 6 or 1 2 3 1 2
    等 所以只需要比较这项是否是上一项+1即可。。。。
    代码如下:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define maxn 100000
     5 using namespace std;
     6 int str[maxn+10];
     7 int main()
     8 {
     9         int t,n,count,i;
    10         scanf("%d",&t);
    11         for(count=1;count<=t;count++)
    12         {
    13             scanf("%d",&n);
    14             for(i=0;i<n;i++)
    15             {
    16                scanf("%d",str+i);
    17             }
    18             if(str[i=0]>0)
    19             {
    20                for(i=1;i<n;i++)
    21                 if(str[i-1]+1!=str[i])
    22                     break;
    23             }
    24            printf("Case #%d: %d
    ",count,(i%n)+1);
    25         }
    26     return 0;
    27 }
    View Code
  • 相关阅读:
    神马搜索 面试小结
    我的第一篇paper
    【转载】技巧:Vim 的纵向编辑模式
    实习求职小结
    将博客园界面打造成Hexo经典主题Light
    试一下Markdown
    四色标记算法
    射雕三部曲的优美片段
    Docker
    Sublime Text 3 文档
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3315217.html
Copyright © 2011-2022 走看看