BestCoder12 1001.So easy(hdu 5058) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5058
(格式有点问题,为了方便阅读~~~整个复制下来吧)


题目意思:给出两个长度都为 n 的集合你,问这两个集合是否相等。
其实思路非常容易想到,就是去重后判断嘛~~~我用到了set 来做。不过有个小细节!!!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
using namespace std; set<int> a, b;
set<int>::iterator pa, pb; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif int n, in;
while (scanf("%d", &n) != EOF)
{
a.clear();
b.clear();
for (int i = ; i < n; i++)
{
scanf("%d", &in);
a.insert(in);
}
for (int i = ; i < n; i++)
{
scanf("%d", &in);
b.insert(in);
} bool flag = true;
pb = b.begin();
for (pa = a.begin(); pa != a.end() && pb != b.end(); pa++, pb++)
{
if (*pa != *pb)
{
flag = false;
break;
}
}
printf("%s\n", flag && pa == a.end() && pb == b.end() ? "YES" : "NO");
}
return ;
}
特别要注意,最后不能单纯只用 flag 来判断输入和输出!!!还需要结合 pa 和 pb 的位置来判断,即都要直到集合结尾,代表元素个数是一样的。因为有可能两个集合去重之后大小不等,而for 循环中是没有考虑到这点的!!只是单纯地以比较短的那个集合来作为基准,如果遇到 {1, 3, 5} , {1, 3, 3},它会提前跳出来并且判断结果是 YES,明显是错的嘛~~~
粗心呀~~~
另外一种解法,unique 妙用,第一次使用,留个纪念吧~~~~
(unique 用之前需要先排序,而且它不是真正的删除,网上说是挪到数组后面而已)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = + ;
int a[maxn], b[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif int n;
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
for (int i = ; i < n; i++)
scanf("%d", &b[i]);
sort(a, a+n);
sort(b, b+n);
int la = unique(a, a+n) - a;
int lb = unique(b, b+n) - b; if (la != lb)
printf("NO\n");
else
{
bool flag = true;
for (int i = ; i < la; i++)
{
if (a[i] != b[i])
{
flag = false;
break;
}
}
printf("%s\n", flag ? "YES" : "NO");
}
}
return ;
}
BestCoder12 1001.So easy(hdu 5058) 解题报告的更多相关文章
- BestCoder12 1002.Help him(hdu 5059) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...
- BestCoder3 1001 Task schedule(hdu 4907) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4907 题目意思:给出工作表上的 n 个任务,第 i 个任务需要 ti 这么长的时间(持续时间是ti ~ ...
- BestCoder5 1001 Poor Hanamichi(hdu 4956) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956(它放在题库后面的格式有一点点问题啦,所以就把它粘下来,方便读者观看) 题目意思:给出一个范围 [ ...
- "1001. A+B Format (20)" 解题报告
Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...
- BestCoder8 1001.Summary(hdu 4989) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...
- BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊---- ...
- BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...
- BestCoder17 1001.Chessboard(hdu 5100) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目意思:有一个 n * n 的棋盘,需要用 k * 1 的瓷砖去覆盖,问最大覆盖面积是多少. ...
- BestCoder13 1001.Beautiful Palindrome Number(hdu 5062) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5062 题目意思:给出 N,找出 1 - 10^N 中满足 Beautiful Palindrome N ...
随机推荐
- 【HDU 5007】Post Robot
Description DT is a big fan of digital products. He writes posts about technological products almost ...
- HTTP各个状态返回值
转载来自于:http://desert3.iteye.com/blog/1136548 502 Bad Gateway:tomcat没有启动起来 504 Gateway Time-out: nginx ...
- 【poj1006】 Biorhythms
http://poj.org/problem?id=1006 (题目链接) 题意 人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天.一个周期内有一天为峰值,在这一天,人在对应的方面 ...
- CSU 1113 Updating a Dictionary
传送门 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Description In th ...
- Redis操作+python
自动化接口测试中需要向redis中插入测试数据: 1. 连接redis: import redisself.r = redis.StrictRedis(host=env.REDIS_HOST, por ...
- CentOS下yum安装VNCserver
VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...
- Extreme Learning Machine(ELM)的工程哲学
Extreme Learning Machine(ELM)的工程哲学 David_Wang2015 发布于2015年5月6日 11:29 工程问题往往需要的是一定精度范围内的结果,而不是“真正的”结果 ...
- The mean shift clustering algorithm
The mean shift clustering algorithm MEAN SHIFT CLUSTERING Mean shift clustering is a general non-par ...
- CSS 2D转换 matrix() 详解
2D转换 IE10.Firefox.Opera 支持 transform 属性 Chrome.Safari 需要前缀 -webkit- . IE9 需要前缀 -ms- . translate():接收 ...
- 32和64位的CentOS 6.0下 安装 Mono 2.10.8 和Jexus 5.0
http://www.cnblogs.com/shanyou/archive/2012/01/07/2315982.html shanyou 博客