类型题目: 数组元素的目标和
C++
#include
#include
#include
using namespace std;
int main()
{
int n,m;
bool find_true=false;
vector money;
cin>>n>>m;
while(n--)
{
int i;
cin>>i;
money.push_back(i);
}
sort(money.begin(),money.end());
for(int i=0,j=money.size()-1;i
{
while(j>=0 && money[j]+money[i]>m) j--;
if(j != i && money[j]+money[i]==m)
{
find_true=true;
cout<
break;
}
}
if(!find_true) cout<
}
Java
import java.util.*;
public class Main
{
public static void main(String[] args) throws Exception
{
Scanner input=new Scanner(System.in);
Boolean find_true=false;
int n=input.nextInt(),m=input.nextInt();
int[] money=new int[n];
for(int i=0;i
Arrays.sort(money);
for(int i=0,j=money.length-1;i
{
while(j>=0 && money[j]+money[i]>m) j--;
if(j!=-1 && j != i && money[j]+money[i]==m)
{
find_true=true;
System.out.printf("%d %d",money[i],money[j]);
break;
}
}
if(!find_true) System.out.printf("No Solution");
}
}
Python3
if __name__ == "__main__":
find_true = False
c=input().split()
n=c[0]
m=int(c[1])
money = [int(x) for x in input().split()]
money.sort()
j=int(n)-1
for i in range(j+1):
while j>=0 and money[j]+money[i]>m:
j=j-1
if j!=-1 and j != i and money[j]+money[i]==m:
find_true = True
print(str(money[i])+" "+str(money[j]))
break
if not find_true:
print("No Solution")