Missing number in array
Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missing number is to be found.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.
The second line of each test case contains N-1 input C[i],numbers in array.
Output:
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 1000
1 ≤ C[i] ≤ 1000
Example:
Input
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output
4
9
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int N,i;
cin>>N;
int *C=new int[N-1];
for(i=0;i<(N-1);i++)
cin>>C[i];
sort(C,C+N-1);
for(i=0;i<N-1;i++)
{
if(C[i]!=i+1)
{
cout<<i+1<<endl;
break;
}
}
if(N==1)
cout<<"1"<<endl;
if(i == N-1)
cout<<i+1<<endl;
}
return 0;
}
Missing number in array的更多相关文章
- LeetCode Array Easy 268. Missing Number
Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...
- [Array]268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...
- [LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
随机推荐
- python学习笔记 list
1.list中的任一元素可以是任一类型.可以是混合的,如,前两个字符串后面的是数字.都是可以的. 2.可以用-1表示最后一个元素. 3.注意不要越界. 4.len(mates) 用来计算list的大小 ...
- iis7 部署网站 403错误
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 403 - 禁止访问: 访问被拒绝. 您无权使用所提供的凭据查看此 ...
- C++反汇编第一讲,认识构造函数,析构函数,以及成员函数
C++反汇编第一讲,认识构造函数,析构函数,以及成员函数 以前说过在C系列下的汇编,怎么认识函数.那么现在是C++了,隐含有构造和析构函数 一丶认识构造函数 高级代码: class MyTest { ...
- ubuntu debain下好用的编辑器
geany: 轻量级的IDE apt-get install geany 用来写shell脚本和python十分方便.特别写python脚本时,它有丰富的提示和自动补全功能.查看代码也很方便
- 数据结构与算法(C/C++版)【绪论/线性表】
声明:数据结构与算法系列博文参考了<天勤高分笔记>.<王道复习指导>.C语言中文网.非商业用途,仅为学习笔记总结! 第一章<绪论> 一.基本概念及入门常识 /// ...
- mysql 打开慢查询日志
打开mysql的配置文件 my.ini或是my.cnf找到节点[mysqld]下添加下面这两行(默认可能不带这两行,直接手敲即可) [AppleScript] 纯文本查看 复制代码 ? 1 2 3 ...
- .NET版支付宝商户会员卡接入
最近公司计划对接支付宝会员卡功能,而任务恰巧由领导安排给我这边,小弟之前也未做过支付宝接口,研究了三天,终于将支付宝会员卡API接口大体上调通了,现将其整理下,以供参考. 蚂蚁金服开发平台-商户会员卡 ...
- Tomcat 源码分析(一)——启动与生命周期组件
写在前面的话:读Tomcat源码也有段时间了,大领悟谈不上.一些小心得记录下来,供大家参考相护学习. 一.启动流程 Tomcat启动首先需要熟悉的是它的启动流程.和初学者第一天开始写Hello Wor ...
- java学习总结篇二--3 种简单排序
本篇文章,先从数据结构开始,一边总结,一边反思,寻求最优解. 本文简单温习下最基础的三类算法:选择,冒泡,插入.先定义一个交换数组作为备用: /** * 交换数组元素 * @param arr * @ ...
- java 邮件发送的公共方法
protected static String host = "true"; protected static String auth = "smtp.163.com&q ...