题目链接: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) 解题报告的更多相关文章

  1. BestCoder12 1002.Help him(hdu 5059) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...

  2. BestCoder3 1001 Task schedule(hdu 4907) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4907 题目意思:给出工作表上的 n 个任务,第 i 个任务需要 ti 这么长的时间(持续时间是ti ~ ...

  3. BestCoder5 1001 Poor Hanamichi(hdu 4956) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956(它放在题库后面的格式有一点点问题啦,所以就把它粘下来,方便读者观看) 题目意思:给出一个范围 [ ...

  4. "1001. A+B Format (20)" 解题报告

    Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...

  5. BestCoder8 1001.Summary(hdu 4989) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...

  6. BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊---- ...

  7. BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...

  8. BestCoder17 1001.Chessboard(hdu 5100) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目意思:有一个 n * n 的棋盘,需要用 k * 1 的瓷砖去覆盖,问最大覆盖面积是多少. ...

  9. BestCoder13 1001.Beautiful Palindrome Number(hdu 5062) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5062 题目意思:给出 N,找出 1 - 10^N 中满足 Beautiful Palindrome N ...

随机推荐

  1. SQL Server中,Numric,Decimal,Money三种字段类型的区别

    都是精确数据类型, 前两个可以自己定义长度和小数位数, Money的定义相当于Numric(19,4) numeric(10,2) 表示最大可以放10位数,但这10位数里有2位是小数如: 123456 ...

  2. java判断list为空

    isEmpty()判断有没有元素而size()返回有几个元素 list.isEmpty()和list.size()==0 没有区别 list!=null跟!list.isEmpty()有什么区别? 这 ...

  3. java中两个Integer类型的值相比较的问题

    今天在做一个算法时,由于为了和其他人保持接口的数据类型一致,就把之前的int换为Integer,前几天测了几组数据,和之前的结果一样,但是今天在测其它数据 的时候,突然出现了一个奇怪的bug,由于之前 ...

  4. DLUTOJ 1331 Maximum Sum

    传送门 Time Limit: 1 Sec  Memory Limit: 128 MB  Description You are given an array of size N and anothe ...

  5. easyui 动态修改窗口title

    http://blog.csdn.net/liu251890347/article/details/39292307?utm_source=tuicool 使用easyui作为前台框架极大的节省了项目 ...

  6. js中的全选,不选,和反选按钮的设定

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

  7. C标准函数库(常用部分)

  8. Swift定义单例

    而在Swift中我们通过清晰的语法便能定义类变量: 通过static定义的类变量无法在子类重写,通过class定义的类变量则可在子类重写. struct SomeStructure { static ...

  9. 运用加密技术保护Java源代码/定制ClassLoader

    为什么要加密? 对于传统的C或C++之类的语言来说,要在Web上保护源代码是很容易的,只要不发布它就可以.遗憾的是,Java程序的源代码很容易被别人偷看.只要有一个反编译器,任何人都可以分析别人的代码 ...

  10. linux查看内核版本、系统版本、系统位数(32or64)

     linux查看内核版本.系统版本.系统位数(32or64) 2011-05-01 22:05:12 标签:linux 内核版本 休闲 系统版本 系统位数 1. 查看内核版本命令: 1) [root@ ...