题目地址:http://ac.jobdu.com/problem.php?pid=1402

题目描述:

现在有n个数,其中有一些出现了一次,一些出现了两次,一些出现了很多次。现在要求你找出那些只出现一次的数,并按升序输出。

输入:

本题有多组case。

每个case有两行,第一行输入一个n,表示有n个数,1<= n <= 1000000。

第二行有n个数字。每个数字的大小范围[1, 1000000]。

输出:

每次输出有两行。

第一行输出一个整数,表示出现一次的数的个数。

第二行按升序输出出现次数为一次的数字,两个数字之间用空格隔开。

样例输入:
5
1 2 2 3 3
7
1 2 2 3 4 4 2
2
2 2
样例输出:
1
1
2
1 3
0

#include <stdio.h>
#include <string.h> #define MAX 1000001 char input[MAX]; int main(void) {
int n;
int i;
int data, cnt;
while (scanf("%d", &n) != EOF){
memset(input, 0, sizeof(input));
for (i = 0; i < n; ++i){
scanf("%d", &data);
if (input[data] == 0){
input[data] = 1;
}
else if (input[data] == 1){
input[data] = -1;
}
}
cnt = 0;
for (i = 1; i < MAX; ++i)
if (input[i] == 1)
++cnt;
printf("%d\n", cnt);
i = 0;
while (i < MAX && cnt != 0){
if (input[i] == 1){
if (cnt != 1)
printf("%d ", i);
else
printf("%d\n", i);
--cnt;
}
++i;
}
}
return 0;
}

位操作

#include <stdio.h>
#include <string.h> #define MAX 125010 unsigned char input[MAX];//标记数字是否出现过
unsigned char num[MAX];//标记数字是否只出现过一次 int main(void) {
int n;
int i;
int data, index, bit, cnt;
while (scanf("%d", &n) != EOF){
memset(input, 0, sizeof(input));
memset(num, 0, sizeof(num));
for (i = 0; i < n; ++i){
scanf("%d", &data);
index = data / 8;//第data位所在单元下标
bit = data % 8;//第data位在下标index单元的bit位
if ((input[index] & (1 << bit)) == 0){
input[index] |= (1 << bit);
}
else if ((num[index] & (1 << bit)) == 0){
num[index] |= (1 << bit);
}
}
cnt = 0;
for (i = 1; i <= 1000000; ++i){
index = i / 8;
bit = i % 8;
if ((input[index] & (1 << bit)) && !(num[index] & (1 << bit)))
++cnt;
}
printf("%d\n", cnt);
i = 1;
while (i <= 1000000 && cnt != 0){
index = i / 8;
bit = i % 8;
if ((input[index] & (1 << bit)) && !(num[index] & (1 << bit))){
if (cnt != 1)
printf("%d ", i);
else
printf("%d\n", i);
--cnt;
}
++i;
}
}
return 0;
}

九度OJ 1402 特殊的数 -- 位操作的更多相关文章

  1. 九度OJ 1209 最小邮票数 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1209 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值.     如,有1分,3分,3分,3 ...

  2. 九度OJ 1214 寻找丑数【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1214 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因 ...

  3. 九度OJ 1183 守形数 (模拟)

    题目1183:守形数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2663 解决:1424 题目描写叙述: 守形数是这样一种整数.它的平方的低位部分等于它本身. 比方25的平方是625. ...

  4. 九度OJ 1214:丑数 (整除)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2180 解决:942 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因 ...

  5. 九度OJ 1060:完数VS盈数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5590 解决:2093 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其为"完数" ...

  6. 九度OJ 1050:完数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7535 解决:3125 题目描述: 求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子1,2,3,1+2+ ...

  7. 九度OJ 1129:Skew数 (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:734 解决:548 题目描述: 在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1).  每个位上的可能数字是0 ...

  8. 【九度OJ】题目1074:对称平方数 解题报告

    [九度OJ]题目1074:对称平方数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1074 题目描述: 打印所有不超过n( ...

  9. 【九度OJ】题目1183:守形数 解题报告

    [九度OJ]题目1183:守形数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1183 题目描述: 守形数是这样一种整数, ...

随机推荐

  1. 对struts2的基本知识和环境的搭建(配图解)

    Struts2的优点: Struts2是由webwork2发展过来的.属于无侵入式设计.而struts1是入侵是设置. Struts2跟servlet API没有紧密的联系. struts2提供了拦截 ...

  2. How to add “Maven Managed Dependencies” library in build path eclipse

    If you have m2e installed and the project already is a maven project but the maven dependencies are ...

  3. 利用Chrome模拟访问移动端网页

    很多网站都通过User-Agent来判断浏览器类型,如果是3G手机,显示手机页面内容,如果是普通浏览器,显示普通网页内容. 谷歌Chrome浏览器,可以很方便地用来当3G手机模拟器.在Windows的 ...

  4. PowerShell中的数学计算

    Double类型和float都属于浮点类型,精度不高.而Decimal属于高精度

  5. HOG特征-理解篇

    网上去找关于HOG的资料,发现理解性的较少,并且较为冗长,为方便大家理解便自己写了篇,希望能对奋斗在特征提取第一线的同志们有所帮助: HOG即histogram of oriented gradien ...

  6. python第三方库系列之十九--python測试使用的mock库

    一.为什么须要mock         在写unittest的时候,假设系统中有非常多外部依赖,我们不须要也不希望把全部的部件都执行一遍.比方,要验证分享到微博的功能,假设每次測试的时候都要真实地把接 ...

  7. 面试题总结之JAVA

    JAVA 1. what is thread safe? 线程安全就是说多线程访问同一代码,不会产生不确定的结果.编写线程安全的代码是低依靠线程同步.线程安全: 在多线程中使用时,不用自已做同步处理线 ...

  8. java_Hashtable实例

    package ming; import java.util.Hashtable; class D { int count; public D(int count) { this.count = co ...

  9. Java_生产者消费者模式

    /* * To change this license header, choose License Headers in Project Properties. * To change this t ...

  10. c#_DropdownList Panel Textbox 控件交互使用,有autopostback和没有的区别

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...