4. Make up

Title description

There are n integers, numbered from 1 to n, to determine whether the sum of two numbers with different numbers is m.

If it can be found, output Yes. If not found, output No.

Note: You need to find two numbers with different numbers.

Input and output format

Input format

The first line of the input is two integers, n and m, and the second line is n numbers.
Where n<=100, the absolute value of the number does not exceed 100000.

Output format

Yes or No

Sample input and output

Input example 1

4 15
1 5 3 10

Sample output 1

Yes

Input example 2

3 5
1 2 5

Sample output 2

No

answer

Water question~
Directly exhaustive, you can easily AC, you do not optimize according to my code, after all, there is not much optimization, the worst complexity is O(n^2), and the average complexity is O(nlogn)

Code

#include<bits/stdc++.h>
using namespace std;
int n,m,a[109];
int main(){
    
    
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++)if(i!=j&&a[i]+a[j]==m){
    
    printf("Yes");return 0;}
	printf("No");
	return 0;
}

Guess you like

Origin blog.csdn.net/JPY_Ponny/article/details/114190308