FJNU-1152 Fat Brother And Integer

Description

Fat brother recently studied number theory, him came across a very big problem — minimum does not appear positive integer. Fat brother get n positive integers, he needs to find out the least a positive integer and the positive integer is not in his n positive integers.

 

Input

There are multiple test cases. The first line of input contains an integer T (T <= 25) indicating the number of test cases. For each test case:

The first line contains a single integer n (1 <= n <= 1000)

The second line contains n positive integer ai ( 1 <= ai <= 1000000000)

 

Output

For each test case, output the minimum positive integer which didn’t appear in the n positive integer

 

 Sample Input

2

5

1 2 3 4 5

5

1 100 101 102 103

 

Sample Output

6

2


即给定数组,输出未出现在该数组中最小的数,注意要先排序,sort的用法。

#include <cstdio>
#include <algorithm>

int main(void)
{
    int t, n;
    int num[1100];
while(scanf("%d", &t) != EOF) { while(t--) { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &num[i]); }
std::sort(num, num+n); int i = 0; if(num[i] > 1) { printf("1\n"); } else { for(; i < n-1; i++) { if(num[i+1]-num[i] > 1) break; } printf("%d\n", num[i]+1); } } } return 0; }

 


STL中自带了排序函数——sort, 通常其表示为 sort(begin, end),即排序的区间、范围(需包涵<algorithm>和std)。

     

int main(void)
{
  int a[5] {1, 4, 6, 2, 3};
  std::sort(a, a+5);
  return 0;
}

 

 sort默认排序方式为从小到大排列,要想从大到小排列,可以自定义一个函数,做为 sort 的第三个参数。

int main(void)
{
    bool func(int, int);

    std::sort(a, a+5, func);
    
    return 0;
}    


bool func(int a, int b)
{
    return a>b;
}

 

 

 

转载于:https://www.cnblogs.com/limyel/p/6600302.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值