首页 > 其他分享 >二分法demo

二分法demo

时间:2023-09-04 21:56:10浏览次数:46  
标签:arr right int demo mid 二分法 res left

1.python实现

from math import floor

arr = [1, 2, 3, 4, 5, 6, 8, 9, 10, 11]
left = 0
right = len(arr)-1
res = 7

while (left <= right):
    mid = floor((left + right)/2)
    if (arr[mid] < res):
        left = mid + 1
    elif (arr[mid] > res):
        right = mid - 1
    else:
        print(f'找到了,下标为{mid}')
        break

if (left > right):
    print('找不到')

2.c语言实现

#include <stdio.h>

int main()
{

    int arr[10] = {1, 2, 3, 4, 5, 6, 8, 9, 10, 11};
    int res = 8;
    int rz = sizeof(arr) / sizeof(arr[0]);
    int left = 0;
    int right = rz - 1;
    int mid = (rz - 1 + 0) / 2;
    while (left <= right)
    {
        mid = (left + right) / 2;
        if (arr[mid] < res)
        {
            left = mid + 1;
        }
        else if (arr[mid] > res)
        {
            right = mid - 1;
        }
        else
        {
            printf("找到了,下标为:%d\n", mid);
            break;
        }
    }
    if (left > right)
        printf("找不到\n");

    return 0;
}

标签:arr,right,int,demo,mid,二分法,res,left
From: https://www.cnblogs.com/hikk/p/17678193.html

相关文章

  • 网页聊天框demo
    效果图css代码body{background-color:gray;}.chat_wrapper{position:absolute;display:flex;width:800px;height:600px;background-color:aliceblue;......
  • 二分法及其变体问题
    描述给定一个n个元素有序的(升序)整型数组nums和一个目标值target,写一个函数搜索nums中的target,如果目标值存在返回下标,否则返回-1。示例1:输入:nums=[-1,0,3,5,9,12],target=9输出:4解释:9出现在nums中并且下标为4示例2:输入:nums=[-1,0,3,......
  • vue组件切换_demo
    项目参考:36_动态组件_哔哩哔哩_bilibili项目结构:App.vue<template><component:is="Component"></component><button@click="switchHandle">switchComponent</button></template><script>importComponent......
  • vue3入门_demo
    新建项目参考:Vuevscode创建vue项目流程【超详细】_vuevscode创建vue项目流程【超详细】_怎么用vscode写vue_一颗不甘坠落的流_一颗不甘坠落的流星的博客-CSDN博客项目结构:App.vue<template><Main></Main></template><script>importMainfrom"./components......
  • Leetcode刷题笔记——二分法
    二分法是搜索算法中极其典型的方法,其要求输入序列有序并可随机访问。算法思想为输入:有序数组nums,目的数值target要求输出:如果target存在在数组中,则输出其index,否则输出-1将原数组通过[left,right]两个索引划分范围,初值left=0,right=数组的最后一个元素当left<=right时mid......
  • 一个CSS动画的demo
       .lineBarDis{height:8px;background-image:linear-gradient(toright,#011c720%,#c7ced2100%);animation:myAnimation1.5sinfinite;/*设置动画为2秒钟的时间,并无限循环*/}@keyframesmyAnimation{0......
  • 拖拽demo
    <!DOCTYPEhtml><html> <head> <metacharset="utf-8"> <title></title> <scriptsrc="js/echarts.5.4.3.js"></script> </head> <body> <divid="main"draggable="......
  • ServiceDemo
    增加(Create)@ServicepublicclassUserService{@AutowiredprivateUserRepositoryuserRepository;publicUserDTOcreateUser(UserDTOuserDTO){Useruser=newUser();//将UserDTO的属性设置到User对象中//...user......
  • Tlist_Demo
    typeTSmt=recordSName:string[16];Feeder:Integer;Dushu:Real;end;Psmt=^TSmt;varList:TList;//List:Tlist<Psmt>;泛型指针TlistusesGenerics.Collections;procedureTForm2.Button1Click(Sender:TObject);varbuf:Ps......
  • 断言Assert.assertEquals(sourceCollection.get(0).getDemoValue(),1);
    Assert.assertEquals(sourceCollection.get(0).getDemoValue(),1);这段代码的作用是验证集合中第一个元素的demoValue属性是否等于预期的值1。如果相等,就没有问题;如果不相等,会抛出异常,表明测试失败或代码出现了问题。......