中科大-东北林业大学.PPT

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
中科大-东北林业大学

二分查找 二分函数直接用---不用自己写了 upper_bound()与lower_bound()使用方法 都是二分函数,头文件algorithm upper_bound返回第一个大于的元素的下标; lower_bound返回第一个大于等于元素的下标; #include iostream #include algorithm//必须包含的头文件 using namespace std; int main(){ ?int point[10] = {1,3,7,7,9}; ?int tmp = upper_bound(point, point + 5, 7) - point;//按从小到大,7最多能插入数组point的哪个位置 ?printf(%d\n,tmp); ?tmp = lower_bound(point, point + 5, 7) - point;////按从小到大,7最少能插入数组point的哪个位置 ?printf(%d\n,tmp); ?return 0; } output: 4 2 看代码---超短 #include iostream #include algorithm #include stdio.h using namespace std; int data[1000008]; int main() { int n,m; while(scanf(%d%d,n,m)!=-1) { for(int i=0;in;i++) scanf(%d,data[i]); int ans=upper_bound(data,data+n,m)-data; coutansendl; } return 0; } //我已经自我陶醉了! 提交后的结果 再看901题,林大OJ 成绩查询 Problem:901 Time Limit:2000ms Memory Limit:65536K Description 给出一组0~100的实数(小数点后保留两位),表示学生成绩,学生个数不超过100000, 求成绩位于区间[s1, s2]的学生人数k。 Input 输入第一行是一个整数n,表示学生个数;接下来一行输入n个数表示n个学生的成绩; 接下来输入多组数据,每组输入两个数s1, s2,输入占一行。(0=s1 s2 =100) Output 对每组输入s1, s2,输出题意描述的k。 Sample Input 10 99.99 100.00 89.45 76.22 63.54 63.54 63.54 0.00 59.99 10.99 0 100 60 70 63.54 100 Sample Output 10 3 7 分析 给定【1 2 3 5 7 9 10】 当区间是 [3,8] [4,9] [2,5]时,在纸上手工计算,是用lower还是upper呢? 分析结果: 给定区间范围[a,b]后,用upper(b)-lower(a) 就行了! 类似题目都这样的,但最好手工分析 代码如下 #include iostream #include algorithm #include stdio.h using namespace std; double data[100002]; int main() { int n; double s1,s2; //scanf(%d,n); cinn; for(int i=0;in;i++) scanf(%lf,data[i]); sort(data,data+n); while(scanf(%lf%lf,s1,s2)!=-1)//必须用scanf,否则超时 { int ans=upper_bound(data,data+n,s2)-lower_bound(data,data+n,s1); printf(“%d\n”,ans);//必须用printf,否则也超时 //coutansendl; 用cout也超时 } return 0; } 容易出错的地方 Int ans=lower_round(a,a+n,x) 这样是错误的! 正确的写法: Int ans=lower_round(a,a+n,x)-a; 因为 lower_round()返回的是指针,而不是int 林大 899题,这也是裸的找 Problem:899 Time Limit:1000ms Memory Limit:65536K Description 我们现在

文档评论(0)

suijiazhuang1 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档