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 ...
随机推荐
- 【BZOJ-1449&2895】球队收益&球队预算 最小费用最大流
1449: [JSOI2009]球队收益 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 648 Solved: 364[Submit][Status][ ...
- bzoj2096 pilots
依旧是维护两个单调队列,只是队首检查的方式略有变动 /*by SilverN*/ #include<iostream> #include<algorithm> #include ...
- 升级OSX 10.9 Mavericks后,会导致Finder始终无响应的一个问题
刚升了OS X 10.9 Mavericks ,发现Finder始终“未响应”(Application Not Responding),查了苹果官网论坛,国内的论坛,解决方法都无效,最后各种尝试,发现 ...
- php返回json数据简单实例
<?php include './include/conn.php'; //数据库链接文件 $sql_notice = mysql_query('SELECT * FROM gg_notice ...
- std::bind(二)
bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板. 用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看 ...
- hdu 2045 不容易系列之(3)—— LELE的RPG难题
解题思路: f(n)=1,2,.....n-2,n-1,n 前n-2个已经涂好,那么n-1有两种可能 1.n-1与n-2和1 的颜色都不同 1 粉, n-2 红, n-1 绿. 那么n的颜色 ...
- block中防止循环引用的一个高大上的宏定义
看惯了什么tempSelf weakSelf,来点高大的 #define weakify(...) \ rac_keywordify \ metamacro_foreach_cxt(rac_weaki ...
- ASP.NET MVC 站点设置.html 为起始页
1. 删除 controller="XX" 2. 确保你的工程根目录下的*.htm或*.html文件名在IIS默认文档中存在 搞定
- Data conversion error converting
词错如果出现在sql语句中,那么多半是类型转换的问题
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...