【练习吧】二分三分算法专项训练

本文介绍了两种算法问题:一是给定三个数列和目标值,判断是否存在三数之和等于目标值;二是求解一个四次多项式方程在0到100范围内的实数解。通过二分查找等算法实现了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--------->> 博主会持续更新ing

A - Can you find it?

 Give you three sequences of numbers A, B, C, then we give you a number X.
  Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.

Input

There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.

Output

For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".

Sample Input

3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

Sample Output

Case 1:
NO
YES
NO

思路:先找第一行和第二行数组的和(sum),再用查找的数(x)减去第三行数组,看是否能在(sum)中找到相等的和,即可。

#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<iostream>
#define maxn 500+10
#define maxm 500*500+10
using namespace std;

int k,x,l,m,n,cas=0;
int lrr[maxn],mrr[maxn],nrr[maxn];
int sum[maxm];

bool f(int x)
{
    int left=0,right=k-1,mid;
    while(left<=right)
    {
        mid=(left+right)>>1;
        if(sum[mid]>x)
        {
            right=mid-1;
        }
        else if(sum[mid]<x)
        {
            left=mid+1;
        }
        else
        {
            return true;
        }
    }
    return false;
}

int main()
{
    while(~scanf("%d%d%d",&l,&m,&n))
    {
        memset(lrr,0,sizeof(lrr));
        memset(mrr,0,sizeof(mrr));
        memset(nrr,0,sizeof(nrr));
        memset(sum,0,sizeof(sum));

        for(int i=0;i<l;i++)
        {
            scanf("%d",&lrr[i]);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d",&mrr[i]);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&nrr[i]);
        }
        k=0;
        int s;
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<m;j++)
            {
                sum[k++]=lrr[i]+mrr[j];
            }
        }
        sort(sum,sum+k);

        scanf("%d",&s);
        printf("Case %d:\n",++cas);
        while(s--)
        {
            scanf("%d",&x);
            bool flag=false;
            for(int i=0;i<n;i++)
            {
                flag=f(x-nrr[i]);
                if(flag)
                {
                    printf("YES\n");
                    break;
                }
            }
            if(!flag)
            {
                printf("NO\n");
            }
        }
    }
    return 0;
}

B - Can you solve this equation?

 Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky. 

Input

The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);

Output

For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.

Sample Input

2
100
-4

Sample Output

1.6152
No solution!

思路:用二分在0-100之间查找,即可。

#include<cstdio>
#include<cmath>

double f(double x)
{
    return (8*x*x*x*x + 7*x*x*x+ 2*x*x + 3*x + 6);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        double m;
        scanf("%lf",&m);
        if(f(0)>m || f(100)<m)
        {
            printf("No solution!\n");
            continue;
        }
        double left=0.0,right=100.0;
        double mid = 50.0;
        while(fabs(f(mid)-m)>=1e-4)
        {
            if(f(mid)>m)
            {
                right = mid;
            }
            else if(f(mid)<m)
            {
                left = mid;
            }
            mid = (left + right)/2.0;
        }
        printf("%.4lf\n",mid);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值