题目链接: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. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  2. 【心得&&体会】

    ★2016.1.1★ 很早就想写这样的一篇blog了,但一直没有抽空去实现,新的一年感觉应该有所改变,故深夜提笔(码字) NOIP卡掉和连续两次月考爆炸,这段时间确实心理不舒服,调节的也不是很到位,但 ...

  3. Linux Cache Mechanism Summary(undone)

    目录 . 缓存机制简介 . 内核缓存机制 . 内存缓存机制 . 文件缓存机制 . 数据库缓存机制 1. 缓存机制简介 0x1: 什么是缓存cache 在计算机整个领域中,缓存(cache)这个词是一个 ...

  4. html,body { margin:0; padding:0;border:0}

    body,html /* 设置窗口DIV为浏览器大小*/ { margin:; padding:; height:100%; } 下面代码 <!DOCTYPE html> <html ...

  5. python二维数组

    和c c++不一样 过程如下: #-*- coding:utf-8 -*- t = [[ 0 for i in range(5)]for j in range(5)] for i in range(5 ...

  6. C++中使用array报错 requires compiler and library surpport for the ISO c++ 2011 standard

    #error This file requires compiler and library support for the \ISO C++ 2011 standard. This support ...

  7. LINUX下为ORACLE数据库设置大页--hugepage

    在Linux中配置hugepage可以提高oracle的性能,减少oracle sga的页交换,类似于aix中的lagepage. 为什么 使用大页? LINUX内存的默认块大小是4K如果SGA为:1 ...

  8. display & visibility区别

    http://www.cnblogs.com/zhangran/archive/2012/08/29/2662459.html 说明:在学习css的过程中由于其中包含太多的元素.属性等等,总有许多是自 ...

  9. Linux下安装Nginx详细图解教程

    什么是Nginx? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,在高连接并发的情况下N ...

  10. ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS

    ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS We recently interviewed ...