This commit is contained in:
parent
f16758019b
commit
2d95dfd953
|
@ -0,0 +1,25 @@
|
|||
# 二分
|
||||
|
||||
[问题描述]
|
||||
|
||||
教学案例,必须使用递归函数完成求解!
|
||||
|
||||
在n个整数(已经按从小到大排好序)中查找m个整数的位置。若存在则输出其位置,若不存在则输出-1。
|
||||
其中( n <= 10^5 ,m <= 10^4 )。
|
||||
|
||||
[输入格式]
|
||||
第1行,两个整数值n和m。
|
||||
第2行,共n个整数值,已经按从小到大顺序排好,数值之间用一个空格间隔,每个值均小于10^9。
|
||||
第3行,共m个整数值,数值之间用一个空格间隔。
|
||||
|
||||
[输出格式]
|
||||
共m行,每行一个整数值,表示找到该数在n个数中的位置(第一个数的位置从1开始编号!),若不存在,输出-1。
|
||||
|
||||
[输入样例]
|
||||
5 2
|
||||
12 25 67 98 123
|
||||
23 67
|
||||
|
||||
[输出样例]
|
||||
-1
|
||||
3
|
|
@ -0,0 +1 @@
|
|||
#
|
|
@ -0,0 +1,49 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
int a[100001];
|
||||
void erfen(int tou, int wei, int n)
|
||||
{
|
||||
int now = wei - tou;
|
||||
if (now < 0||tou>wei) {
|
||||
cout << "-1" << endl;
|
||||
return;
|
||||
}
|
||||
if (now == 0) {
|
||||
if (a[tou+now] == n) {
|
||||
cout << tou+now << endl;
|
||||
return;
|
||||
} else {
|
||||
cout << "-1" << endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
now /= 2;
|
||||
now+=tou;
|
||||
//cout<<tou<<" "<<wei<<" "<<now<<endl<<endl;
|
||||
if (a[now] == n) {
|
||||
cout << now << endl;
|
||||
return;
|
||||
}
|
||||
if (n>a[now]) {
|
||||
erfen(now + 1, wei, n);
|
||||
return;
|
||||
} else {
|
||||
//cout<<"<";
|
||||
erfen(1, now - 1, n);
|
||||
return ;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int zong, zhao;
|
||||
cin >> zong >> zhao;
|
||||
for (int i = 1; i <= zong; i++) {
|
||||
cin >> a[i];
|
||||
}
|
||||
for (int i = 1; i <= zhao; i++) {
|
||||
int now;
|
||||
cin >> now;
|
||||
erfen(1, zong, now);
|
||||
// cout<<now<<endl;
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,3 @@
|
|||
200 20
|
||||
1 360 1129 2497 2821 3223 5194 5611 7223 8704 9220 9266 10410 10615 13429 13618 13725 15201 15887 16862 17773 18433 19153 20252 20665 23233 23625 23657 24381 24697 25283 26197 26352 27136 29009 30711 31091 31361 32217 39921 40585 40886 41298 41497 42049 43777 46005 46093 46181 46593 47437 47803 50985 51781 52235 54406 56027 56655 57541 58201 58945 59771 60348 61216 62203 62293 65109 65367 65445 66083 71425 74304 74306 74341 74407 77163 77781 79806 80897 81208 83981 85119 85945 87745 89197 89571 92551 94601 94897 95953 96305 98395 100801 101221 104449 104651 105694 106057 107563 109940 110173 110365 110755 111137 112376 112764 113851 116111 116755 117131 117909 118249 118365 118993 119691 120033 120259 121563 122231 125786 127261 128761 128811 129193 130243 133240 134107 134408 134824 135304 136590 136732 137407 137701 138265 139245 141001 141254 141362 142317 144093 145187 146901 148198 148282 148679 149416 149431 151553 151697 152657 153448 153841 153902 154113 155145 155521 157543 158001 158167 158865 159062 160447 160594 164901 168265 168849 169507 169671 169690 172481 174733 175150 175286 175957 176347 180665 180901 182211 182645 186494 186581 186746 187009 187061 187071 188671 188937 189255 190233 190939 191441 191491 194371 195321 195824 196120 196197 197991 199221
|
||||
196209 6546 104767 167211 48316 130606 86481 92009 189721 52717 31453 141637 194352 177067 199915 20329 100285 195325 63501 112889
|
|
@ -0,0 +1,3 @@
|
|||
100 10
|
||||
1 739 2326 3673 5361 8641 9447 10829 12001 12091 15016 17863 19291 19782 22601 22617 23266 24511 28477 31185 32897 33881 34113 34546 37675 38257 38307 41050 41176 43595 44731 46231 46632 46831 55105 56001 57861 59929 61038 62143 62182 67211 72377 79489 80746 86481 88383 88529 88675 91489 91769 91904 95311 97632 99793 101179 101901 109558 111895 112465 118541 118951 120798 123934 124585 129406 131804 132607 136033 136943 140311 141233 143326 149153 151030 153357 154138 156069 159733 160798 161439 161569 162709 167311 168247 171349 171799 172593 173777 174581 178144 178649 178666 181987 183464 186757 187417 188253 191591 192682
|
||||
19573 16246 140383 178201 199721 140532 41777 131643 112501 105769
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue