• 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
  • 相关阅读:
    轻松学习Linux之AWK使用初步
    轻松学习Linux之理解Shell的硬链接与软连接
    轻松学习Linux之自动执行任务
    轻松学习Linux系统安装篇之fdisk命令行工具的使用
    Leetcode-1030 Next Greater Node In Linked List(链表中的下一个更大节点)
    Leetcode-1028 Convert to Base -2(负二进制转换)
    Leetcode-1029 Binary Prefix Divisible By 5(可被 5 整除的二进制前缀)
    ACM模板——2的次方表
    ACM模板——快速幂
    ACM模板——素数相关
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3315217.html
Copyright © 2020-2023  润新知