7-1 二分查找（20 分）

4
1 2 3 4
1

0
2

4个回答

`````` #include <iostream>

using namespace std;
void test(int a[],int n,int v);
int main()
{
int n,i=0,v;
int a[1000];
cin>>n;
while(i!=n)
{
cin>>a[i++];
}
cin>>v;
test(a,n,v);
return 0;
}
void test(int a[],int n,int v)
{
int i = 0;
int index = n;
while(index!=0){
index/=2;
if(a[index]==v){
cout<<index<<"\n"<<i<<"\n";
return;
}else
{
i++;
}
}
cout<<-1<<"\n"<<i<<"\n";
}
``````

``````#include <iostream>
#include <algorithm>
using namespace std;

const int MAX = 1000 + 10;

typedef struct {
int val, id; //分别为值和下标位置
}Point;

int n;
Point a[MAX];
int num;

//将a[ ]按值的大小从小到大排序
bool cmp(Point x, Point y)
{
if (x.val == y.val)
return x.id < y.id;
return x.val < y.val;
}

int bin_search(int x)
{
int l = 0;
int r = n;
while (r - l >= 1)
{
int i = (l + r) >> 1;
if (a[i].val == x)
return a[i].id;
else if (a[i].val < x)
l = i + 1;
else
r = i;
num++;
}
return -1;
}

int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
cin >> a[i].val;
a[i].id = i;
}
sort(a, a + n, cmp);
int x;
cin >> x;
num = 0;
int pos = bin_search(x);
cout << pos << endl;
cout << num << endl;
return 0;
}

``````

#include

using namespace std;
void test(int a[],int n,int v);
int main()
{
int n,i=0,v;
int a[1000];
cin>>n;
while(i!=n)
{
cin>>a[i++];
}
cin>>v;
test(a,n,v);
return 0;
}
void test(int a[],int n,int v)
{
int i = 0;
int index = n;
while(index!=0){
index/=2;
if(a[index]==v){
cout<<index<<"\n"<<i<<"\n";
return;
}else
{
i++;
}
}
cout<<-1<<"\n"<<i<<"\n";
}

`````` #include <iostream>

using namespace std;
void test(int a[],int n,int v);
int main()
{
int n,i=0,v;
int a[1000];
cin>>n;
while(i!=n)
{
cin>>a[i++];
}
cin>>v;
test(a,n,v);
return 0;
}
void test(int a[],int n,int v)
{
int i = 0;
int index;
int low = 0;
int high = n-1;
while(low<=high){
index = (low + high)/2;
if(low == n-2 && high == n-1) index = n-1;//边界处理
if(a[index]==v){
i++;
cout<<index<<"\n"<<i<<"\n";
return;
}else if(a[index]>v)
{
i++;
if(index == 0) break;//如果a[0]大于v，那么就不在范围内
high = index;

}else
{
i++;
if(index == n-1) break;//如果a[n-1]小于v，那么就不在范围内
low = index;
}
}
cout<<-1<<"\n"<<i<<"\n";
}
``````

7-1 二分查找（20 分） 输入n值(1<=n<=1000)、n个整数以及要查找的数x，使用二分查找算法查找x，输出x所在的下标（0~n-1）及比较次数。若x不存在，输出-1和比较次数。 输入格式: 输入共三行： 第一行是n值； 第二行是n个整数； 第三行是x值。 输出格式: 输出x所在的下标（0~n-1）及比较次数。若x不存在，输出-1和比较次数。 输入样例: 4 1 2 3 4 1 输出样例: 0 2

C++程序编写，求解求解

C语言新人求解这道题怎么编写

laypage，求解，急急急急急急急

Problem Description “JezzBall is a computer game in which red-and-white ‘atoms’ bounce about a rectangular field of play. The player advances to later levels (with correspondingly higher numbers of atoms and lives) by containing the atoms in progressively smaller spaces, until at least 75% of the area is blocked off.” (wikipedia.org) The picture to the right is a screenshot from the original game, where the player has already covered some space (the black part). In this problem we will consider a slightly different, non-discrete, version of the game. That is, while the length unit is still pixels, you should treat them as non-discrete in the sense that all objects can be at non-integer coordinates and all movements are continuous. The size of the playing field will be 1024 × 768 pixels. The atoms that bounce around will be infinitely thin (and not round balls like in the screenshot). The atoms will move at a constant speed and only change direction when hitting the edge of the playing field (x-coordinate 0 and 1024 or y-coordinate 0 and 768), where they bounce without loss of energy. The atoms do not hit each other. The player can divide the playing field in two by shooting a horizontal or vertical ray from (in this problem) a fixed point on the playing field. The ray will then extend in both directions simultaneously (up and down for vertical rays, or left and right for horizontal rays) at a uniform speed (in this problem always 200 pixels per second). The rays will also be infinitely thin. If no atom touches any part of the ray while it’s still being extended, the field has successfully been divided. Otherwise the player loses a life. If an atom touches the endpoint of an extending edge, this will not be counted as a hit. Also, if an atom hits the ray at the same instant it has finished extending, this will also not count as a hit. Write a program that determines the minimum time the player must wait before he can start extending a ray so that an atom will not hit it before the ray has been completed. Input Each test case starts with a line containing a single integer n, the number of atoms (1 ≤ n ≤ 10). Then follows a line containing two integers, x and y, the position where the two ray ends will start extending from (0 < x < 1024, 0 < y < 768). Then n lines follow, each containing four integers, x, y, vx and vy describing the initial position and speed of an atom (0 < x < 1024, 0 < y < 768, 1 ≤ |vx| ≤ 200, 1 ≤ |vy| ≤ 200). The speed of the atom in the x direction is given by vx, and the speed in the y direction is given by vy. All positions in each input will be distinct. The input is terminated by a case where n = 0, which should not be processed. There will be at most 25 test cases. Output For each test case, output the minimum time (with exactly 5 decimal digits) until the player can extend either a horizontal or vertical ray without an atom colliding with it while it is being drawn. The input will be constructed so that the first time this occurs will be during an open interval at least 10-5 seconds long. If no such interval is found during the first 10000 seconds, output “Never” (without quotes). Sample Input 3 700 420 360 290 170 44 900 150 -53 20 890 100 130 -100 4 10 10 1 1 192 144 513 385 192 144 1023 767 -192 -144 511 383 -192 -144 0 Sample Output 2.80094 Never

Problem Description Alice has a sequence {a1,a2,…,an} with n positive integers. Bob has some positive integer sequences with different size. Alice wants to know the total occurrences of every sequence Bob has in Alice's sequence (the occurrences are allowed to overlap). We say one sequence B occurs in another sequence A if there is a contiguous subsequence of A that is the same as B after scaled by a positive real factor. For example A={2,4,8,16},B={1,2} then B occurs three times in A. The occurrences are {2,4}, {4,8} and {8,16}. And the factor is 0.5, 0.25 and 0.125. Input There are multiple test cases. The first line of input contains an integer T (1≤T≤30), indicating the number of test cases. For each test case: The first line contains two integer n and m (1≤n,m≤100000), indicating the size of Alice's sequence and the number of sequences Bob has. In the next line, there are n integers, a1,a2,…,an, indicating Alice's sequence. In the following m lines, each starts with an integer ki(1≤ki≤300000) - the size of the sequence. Then ki space separated positive integers follow, indicating the sequence. The total sum of ki is less than or equal to 1000000. Other integers are between 1 and 10000, inclusive. Output For each test case, output a single line with a single integer, indicating the total number of occurrences. Sample Input 2 4 1 2 4 8 16 2 1 2 5 3 2 4 2 4 6 3 1 2 1 1 5 2 16 8 Sample Output 3 7

C语言 在编写链表是时候出现了这样的问题 求解

Problem Description In the popular TV series Heroes, there is a tagline "Save the cheerleader, Save the world!". Here Heroes continues, "Solve the puzzle, Save the world!". Finally, alien invaders visit our planet. They are eccentric and launch attack many rounds. Since trust in prime numbers, each round they send out p killers, here p is a prime number. Countries on our planet unite and assemble an armed troop of n population. And fortunately we get a fatal weakness of enemy from a betrayer. If the ways of choosing m warriors from n is a multiple of p, the killer number, we will win. Otherwise, we will lose. As the greatest programmer of our planet, you are invited to write a program to count the number of m(0≤m≤n) such that C(n, m) is a multiple of prime p. Input Each line will contain an integer n(1≤n≤10^5) and a prime p(2≤p<10^7), separated by a single space. Process to the end of file. Output For each test of case, if the world can be saved, output the number of ways, otherwise, output "Where is hero from?"(without quotation), both on a single line. Sample Input 6 2 5333 127 100000 11 Sample Output 3 Where is hero from? 92301

Java语言求一元二次方程的两个实根的程序的编写

Java语言求一元二次方程的两个实根的程序的编写，要有完整的代码和相关的注释说明。

``` # include <stdio.h> int main() { int trans(char); char c; printf("输入一串字符串进行翻译:\n"); c = getchar(); while(c != '\n') { c = trans(c); printf("%d", c); getchar(); } putchar('\n'); return 0; } trans(char ch) { if(ch >= 65 && ch <= 86 || ch >= 97 && ch <= 118) ch += 4; if(ch >= 87 && ch <= 90 || ch >= 119 && ch <= 122) ch -= 22; return ch; } ``` 这里我运行以后会出现这样的情况： ![图片说明](https://img-ask.csdn.net/upload/201508/07/1438914665_358848.jpg) **就是我只输入了4个字符，结果却输出了5个，这是怎么回事啊？？** 菜鸟正在学习，跪求大神解答！

2020阿里全球数学大赛：3万名高手、4道题、2天2夜未交卷

HashMap底层实现原理，红黑树，B+树，B树的结构原理 Spring的AOP和IOC是什么？它们常见的使用场景有哪些？Spring事务，事务的属性，传播行为，数据库隔离级别 Spring和SpringMVC，MyBatis以及SpringBoot的注解分别有哪些？SpringMVC的工作原理，SpringBoot框架的优点，MyBatis框架的优点 SpringCould组件有哪些，他们...

《Oracle Java SE编程自学与面试指南》最佳学习路线图2020年最新版（进大厂必备）

《Oracle Java SE编程自学与面试指南》最佳学习路线图（2020最新版）