首页 > 其他分享 >C语言每日一题——第十二天

C语言每日一题——第十二天

时间:2022-10-09 01:55:05浏览次数:69  
标签:第十二天 target int lo 每日 C语言 hi func else

第十二天

小明今天要挑战一下算法!他的算法第一课是:二分查找。

小明随意设置了一个函数:\(y=x^2+2x-1,x\in(-10^4, 10^4)\)。他将使用二分法,找出给出的数字所在区间,精度为\(\pm1\)。

输入

程序运行时通过scanf获取一个整型,作为给定数。

输出

打印给定数所在函数区间,精度为\(\pm1\)。若给定数有不只一个解,打印任意一个区间即可;若给定数恰好被二分查询到,直接打印对应的\(x\)值即可;若给定数不在值域内,请打印error

样例

Input a number:1
 (0, 1)
Input a number:100019999
 10000
Input a number:-1
 0
Input a number:-4
 error

关键

算法基础

提示

“二分法”相关链接:二分法一文全搞定


解析

#include <stdio.h>
#include <iso646.h>


int func(int x);

void dichotomy(int target, int lo, int hi, int is_increment);

int main() {
    int y;
    printf("Input a number: ");
    scanf("%d", &y);

    // 由于在定义域里, f(x) 并非单调函数,我们应该手动判断区间。
    if (y < -2) {
        dichotomy(y, -10001, 0, 0);
    } else if (y == -2) {
        printf("-1\n");
    } else {
        dichotomy(y, -2, 10001, 1);
    }

    return 0;
}


int func(int x) {
    return x * x + 2 * x - 1;
}


/**
 * @brief              : 二分查找 target = func(x), x in (lo, hi); 将结果打印出来
 * @param target       : 目标值
 * @param lo           : 范围较小值
 * @param hi           : 范围较大值
 * @param is_increment : 函数是否递增
 */
void dichotomy(int target, int lo, int hi, const int is_increment) {
    int x, y;

    // 判断数字是否合理
    if (is_increment) {
        if (func(lo) > target or func(hi) < target) {
            printf("error");
            return;
        }
    } else {
        if (func(hi) > target or func(lo) < target) {
            printf("error");
            return;
        }
    }

    while (lo + 1 < hi) {
        x = (hi + lo) / 2;
        y = func(x);
        if (y == target) {
            // 恰巧找到了这个数
            printf("%d\n", x);
            return;
        } else if (y < target) {
            // y 小于目标值
            if (is_increment) {
                lo = x;
            } else {
                hi = x;
            }
        } else {
            // y 大于目标值
            if (is_increment) {
                hi = x;
            } else {
                lo = x;
            }
        }
    }
    printf("(%d, %d)\n", lo, hi);
}

标签:第十二天,target,int,lo,每日,C语言,hi,func,else
From: https://www.cnblogs.com/tobe-goodlearner/p/basic_C_programming-day_12.html

相关文章

  • 【10月】C语言学习第1天
    指针符号&和*&用于指向变量数据位置,用十六进制表示*用于指向变量内存储的值-----------------------------------------函数对变量进行操控:由于函数返回只有一个值,固......
  • 每日一结
    576.出界的路径数采用剪枝和记忆搜索的方法。当加上dir之后的坐标值,越界时,说明找到了出路,此时return1;当没有移动步数的时候,直接return0;当当前的坐标值加/减移动步数......
  • C语言 初识C语言04
    常量C语言中的常量分为以下几种:1、字面常量:直观写出来的值intmain(){3;//字面常量3=5;//error,常量的值不能被改变return0;}2、const修饰的常变量#include<stdio.h>intm......
  • 扫雷游戏---手把手教程(含源码)【C语言】
    ......
  • C语言新知识点:枚举变量enum
    我们可以定义一个变量,然后进行判断inta;if(a==1){}else{}但上面的方式导致变量以数值方式表示,晦涩难懂可以考虑用宏定义#defineYes1但是当范围不同......
  • c语言练习
    //计算n的阶乘#include<stdio.h>intmain(){ inti=0; intn=0; intret=1; scanf("%d",&n); for(i=1;i<=n;i++) { ret=ret*i; } printf("ret=%d\n",ret); return0;}//......
  • [C语言] 初始字符串
    ......
  • 【C语言_16】初步了解指针
    前言每一个变量都有一个内存位置,每一个内存位置都定义了,可使用 &(取地址运算符)运算符访问的地址,它表示了在内存中的一个地址。&的用法:&数据对象//获取数据对象首地址和所需......
  • 【我开了C语言的金手指】三.数组
    (一)一维数组的创建和初始化1.数组的创建数组是一组相同类型元素的集合,其创建方式为:​​type_tarr_name[const_n]​​注:type_t是数组类型const_n是一个常量表达式,用来指定数......
  • C语言每日一题——第十一天
    第十一天还记得在第三天写的斐波那契数列程序吗?小明最初想用这个函数作为随机数生成器的。今天,小明决定重新拾起这个函数,用于生成随机数……输入程序在运行时通过getcha......