题目:Duplicate Pair

大意:

有多组测试数据,输入整数n,接着输入n个整数(integers),这些数字几乎都只出现一次,但其中有一个数出现了两次,请输出这个数。

题解:

大数据问题,普通的两个for暴力解决很明显会T,也不能用C++的cin和cout(费时太多)。那么这个问题就转换成如何省时间的一题。

代码:

#include <stdio.h>
#include <string.h>
#define N 1000010 char a[N]; //定义字符数组,用到memset,节省时间 int main(void)
{
int n;
int i;
int j;
while(scanf("%d", &n) != EOF)
{
memset(a, 0, n);
for(i = 0; i < n; i++)
{
scanf("%d", &j);
if(a[j] == 0) a[j] = 1;
else printf("%d\n", j);
}
}
return 0;
}

思路分析:

既然你输入n个数,我就开个n大小的字符数组,数组中所有元素置0,(为省时用memset,所以这里定义的是字符数组,调用#include<string.h>)你输入的每一个数比如说j对应的字符数组的成员是a[j]a[j]代表的就是你的状态,输入j以后如果之前没有和j相同的数(也就是a[j]==0),将其值置为1,代表这个数出现了;如果这个数之前出现了(a[j]==1),就得到我们要的数了。

FOJ-1001-Duplicate Pair的更多相关文章

  1. 1001 Duplicate Pair

    1.题目戳这里 2.代码: #include<stdio.h> #include<string.h> int main() { int n; while(scanf(" ...

  2. QEMU KVM libvirt 手册(1): 安装

    安装 对虚拟化的支持通常在BIOS中是禁掉的,必须开启才可以. 对于Intel CPU,我们可以通过下面的命令查看是否支持虚拟化. # grep "vmx" /proc/cpuin ...

  3. [CSP-S模拟测试]:Six(数学)

    题目传送门(内部题85) 输入格式 一个正整数$N$. 输出格式 一个数表示答案对$1000000007$取模后的结果 样例 样例输入1: 样例输出1: 样例输入2: 样例输出2: 样例输入3: 样例 ...

  4. 1001. A+B Format (20)

    原题连接:https://www.patest.cn/contests/pat-a-practise/1001 题目如下: Calculate a + b and output the sum in ...

  5. 220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  7. PAT甲级 1001 A+B Format

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...

  8. 详解OJ(Online Judge)中PHP代码的提交方法及要点【举例:ZOJ 1001 (A + B Problem)】

    详解OJ(Online Judge)中PHP代码的提交方法及要点 Introduction of How to submit PHP code to Online Judge Systems  Int ...

  9. s - t 平面图最大流 (附例题 bzoj 1001)

    以下均移自 周冬的<两极相通-浅析最大最小定理在信息学竞赛中的应用> 平面图性质 1.(欧拉公式)如果一个连通的平面图有n个点,m条边和f个面,那么f=m-n+2 2.每个平面图G都有一个 ...

随机推荐

  1. mysql 内置功能 存储过程介绍

    存储过程介绍 就是mysql内置功能把逻辑写好 的功能给封装好,封装成一个接口名,把接口名丢给应用程序,应用程序直接调用接口名实现一系列增删改查功能 这个接口叫存储过程 基于存储过程封装成一个功能 存 ...

  2. HandlerSocket

    HandlerSocket http://www.uml.org.cn/sjjm/201211093.asp 目录: HandlerSocket的原理 HandlerSocket的优势和缺陷阐述 Ha ...

  3. 003-redis-命令-key操作,字符串操作

    Redis 键(key) Redis 键命令用于管理 redis 的键. 序号 命令及描述 1 DEL key该命令用于在 key 存在时删除 key. 2 DUMP key 序列化给定 key ,并 ...

  4. java-小技巧-001-Long序列化到前端js不支持

    1.引入:jackson-mapper-asl-1.9.2.jar 2.导入: import org.codehaus.jackson.map.annotate.JsonSerialize;impor ...

  5. office 2016 install(office2016组件自定义安装激活程序) v5.9.3中文绿色版

    下载地址  http://www.ddooo.com/softdown/71741.htm#dltab office 2016 install是目前下载office2016和office2016组件最 ...

  6. python 根据路径导入模块

    Import python module NOT on path http://stackoverflow.com/questions/10161568/import-python-module-no ...

  7. hover()与toggle()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. vue.js使用typescript踩坑记

    最近在把https://github.com/renrenio/renren-fast-vue这个项目转为typescript,在此记录一下遇到的小坑 name坑:属性该怎么给? 声明文件坑:如何解决 ...

  9. Oracle存储过程中游标的简单使用

    存储过程中查询语句如何返回多行结果? 我们知道,如果存储过程中查询语句有多行结果输出,会报错:ORA-01422: exact fetch returns more than requested nu ...

  10. js时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...