--------->> 博主会持续更新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;
}