题目链接: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. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

  2. ORACLE创建表空间、创建用户、更改用户默认表空间以及授权、查看权限

    Oracle创建用户.表空间.导入导出....命令 //创建临时表空间 create temporary tablespace ext_temptempfile 'D:\oracle\product\ ...

  3. POJ3038 Flying Right

    Description Figuring that they cannot do worse than the humans have, Farmer John's cows have decided ...

  4. IIS7部署项目时提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败。"的解决办法

    这个错误的定位:你的站点使用了Forms验证,而且在部署在生产环境的时候,设置错误,或者注释了. 解决方法如下: 1.检查Forms配置是否屏蔽. 2.有权限访问的资源是否已经开发. 基本就围绕以上两 ...

  5. TCP/IP详解 笔记九

    广播和多播 多播和广播只能用于UDP包,TCP明确在两个进程间建立连接. 多播:帧只传送给属于多播组的多个接口 主机对帧的过滤过程: 通常网卡只接收那些目的地址为本物理接口地址或广播地址的帧:设置为混 ...

  6. Syntax error, annotations are only available if source level is 1.5

    在项目上右键 -> Properties -> Java Compiler

  7. centos6.3配置MFS服务器

    一.简介 MooseFS(Moose File System,mfs)是一种分布式文件系统,它将数据分布在网络中的不同服务器上,支持FUSE(用户空间文件系统Filesystem in Userspa ...

  8. 检验php用时

    <?php// 实例1 /** * @start time */function proStartTime() { global $startTime; $mtime1 = explode(&q ...

  9. spring事务学习(转账案例)(一)

    一.创建数据库并插入数据 create database spring_transaction; use spring_transaction; create table account( id in ...

  10. 使用.NET FrameWork获取CPU,内存使用率以及磁盘空间

    在以前,我们想获取CPU,内存等信息就不得不借助win32 API来实现.但现在,.NET FrameWork已经把这些API封装到.NET类库中了,所以我们可以借助.NET类库很轻松的获取这些信息. ...