摘要: 标准库里的count_if可以统计容器中满足特定条件的元素的个数。例如要统计一个整数vector——ivec中正数的个数,可以先写一个返回类型为bool,含有一个int参数的条件函数:bool pred(int val)...{return val>0;} 之后可以用count_if(ivec.begin(),ivec.end(),pred)计算出正整数的个数。但这个方法有一个明显的缺陷:如果要统计大于10的个数、大于100的个数……就要写很多个类似的函数,能不能进一步抽象?如果能像这样调用count_if:count_if(ivec.begin(),ivec.end,pred(n)), 阅读全文
posted @ 2013-04-20 17:56 PegasusWang 阅读(1840) 评论(0) 推荐(0)
摘要: 这是Titan Ruins系列第一道题,以后慢慢更新。赤裸裸滴阅读理解题,大意就是找到三个连在一起的数,使其之和最大,输出的第一个数是这三个数的和,第二个数是中间那个数所在的位置。水题一道,很简单。 1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 int main() 5 { 6 int n; 7 cin >> n; 8 int *a = new int[n+1]; 9 for (int i = 1; i <= n; ++i)10 cin >> a[i]; 阅读全文
posted @ 2013-04-20 17:10 PegasusWang 阅读(458) 评论(0) 推荐(0)
摘要: 第一次写这种题,参考一下网上的代码。 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 int dir[][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}}; //四个方向 6 int n, m; 7 const int MAXN = 105; 8 struct Node 9 {10 int x, y, time;11 friend bool operator<(Node a, Node b) //重载 阅读全文
posted @ 2013-04-20 15:04 PegasusWang 阅读(177) 评论(0) 推荐(0)
摘要: 今天又重新回顾了一下忘得差不多的并查集:下边是转的,再加上一道hdu的并查集入门题。文章作者:yx_th000文章来源:Cherish_yimi(http://www.cnblogs.com/cherish_yimi/) 转载请注明,谢谢合作。 [本文新址:http://www.ahathinking.com/archives/10.html]昨天和今天学习了并查集和trie树,并练习了三道入门题目,理解更为深刻,觉得有必要总结一下,这其中的内容定义之类的是取自网络,操作的说明解释及程序的注释部分为个人理解。并查集学习:l并查集:(union-find sets)一种简单的用途广泛的集合. 并 阅读全文
posted @ 2013-04-20 00:02 PegasusWang 阅读(598) 评论(0) 推荐(1)
摘要: 本人的第一题bfs搜索:在一个矩形区域内,有些地方有水,有些地方没水。所有相邻的有水的地方会共同组成一个水洼,小蝌蚪想在这块区域中找到一个最大的水洼来安家。Input有多组输入数据,每组第一行包含两个正整数n,m(n,m<=100),接下来n行,每行m个字符,“.”表示有水,“#”表示没水。Output对于每组输入数据输出一行,包含一个整数,表示最大的水洼的面积。Sample Input33########.23#....#33##.#...##Sample Output143 1 #include <iostream> 2 #include <cstdio> 3 阅读全文
posted @ 2013-04-12 20:55 PegasusWang 阅读(476) 评论(0) 推荐(0)
摘要: 刚接触搜索,这一题直接看蒙了,上网查的解题报告,重新理一下思路。首先当所有木棒总长度不能被4整除时以及木棒最大长度大于总长度除以4时,不能组成正方形,直接输出no。深搜时从第一个开始往后搜索,只要满足当前边长+当前木棒长<正方形边长,就标记该木棒,并继续搜索后面的木棒,当木棒长度=sum/4 时,count加1,当count=3时表明能够成正方形,flag=1,返回,flag=0则不能组成正方形。 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespa 阅读全文
posted @ 2013-04-08 21:41 PegasusWang 阅读(670) 评论(0) 推荐(0)
摘要: 一开始直接模拟,果断超时了,后来上网查了查,是组合数学中的置换群。本题求经过多少次置换P能变成初始状态。对于任意一个[1,n]的数字,经过多次置换后一定可以变会初始状态,而且置换的次数不会超过n。对于第i个数,模拟计算出其置换周期,记为ai,答案就是所有ai的最小公倍数。 1 2 3 4 5 4 1 5 2 3次数:3 3 2 3 2,最小公倍数6. 1 #include <iostream> 2 #include <cstdio> 3 int gcd(int a, int b) 4 { 5 if (b == 0) 6 return a; 7 else ... 阅读全文
posted @ 2013-04-07 11:50 PegasusWang 阅读(294) 评论(0) 推荐(0)
摘要: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 struct Time 5 { 6 int hour; 7 int minute; 8 int angle; 9 };10 double compute_angle(Time a)11 {12 if (a.hour >= 12)13 a.hour -= 12;14 int t = abs(a.hour*60 + a.minute - a.minute*12); //乘以2避免浮点数运算15... 阅读全文
posted @ 2013-04-06 17:43 PegasusWang 阅读(494) 评论(0) 推荐(0)
摘要: 以下是基于图的链表表示的:dfs和bfs的演示:http://sjjg.js.zwu.edu.cn/SFXX/sf1/gdyxbl.html (深搜)http://sjjg.js.zwu.edu.cn/SFXX/sf1/sdyxbl.html (广搜)bfs通过检测边发现点,被发现点(但未探索)入队。(被探索是指是否检测过与该点相关联的临近顶点)一个顶点被完全探索当且仅当他的所有边被检测。一个顶点探索完选另一个顶点,被选点应位于被发现但未被探索点队列的队首。待探索点集为空时算法结束。(bfs探索顺序与发现顺序一致,dfs发现后马上探索) 1 #include <iostream> 阅读全文
posted @ 2013-04-06 15:55 PegasusWang 阅读(25193) 评论(2) 推荐(1)
摘要: 题目链接http://acm.timus.ru/problem.aspx?space=1&num=1033唯一注意的就是当图不连通时两个角都要搜索,搜索的总和才是最终答案。#include <iostream>#include <cstdio>#include <cstring>int n, num;char map[34][34];bool visited[34][34] = {0};int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};void dfs(int x, int y){ if ((x==0 &a 阅读全文
posted @ 2013-04-06 14:28 PegasusWang 阅读(374) 评论(0) 推荐(0)