# 【九度OJ1352】|【剑指offer41】和为S的两个数字 原

aqia358

``````import java.io.BufferedReader;
import java.io.IOException;
import java.io.StreamTokenizer;

public class Main {

public static void find(int[] a,int target){
int m = 0;
int n = 0;
boolean flag = false;
for(int i = 0; i < a.length; i++){
for(int j = i+1; j < a.length; j++){
if(a[i] * 2 > target){
i = a.length;
j = a.length;
break;
}
int temp = a[i] + a[j];
if(temp > target){
break;
}else if(temp == target){
m = a[i];
n = a[j];
flag = true;
i = a.length;
j = a.length;
break;
}
}
}
if(!flag){
System.out.println("-1 -1");
}else{
if(m > n)
System.out.println(n+" "+m);
else
System.out.println(m+" "+n);
}

}
public static void find2(int[] a, int target){
int low = 0;
int high = a.length -1;
while(low < high){
if(a[low] + a[high] == target)break;
else if(a[low] + a[high] < target)
low++;
else
high--;
}
if(low < high)
System.out.println(a[low]+" "+a[high]);
else
System.out.println("-1 -1");
}

public static void main(String[] args) throws IOException {
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
while(st.nextToken() != st.TT_EOF){
int n = (int) st.nval;
st.nextToken();
int target = (int) st.nval;
int[] a = new int[n];
int count = 0;
while(count < n){
st.nextToken();
a[count++] = (int) st.nval;
}
find2(a, target);
}
}

}``````

### aqia358

[算法总结] 13 道题搞定 BAT 面试——字符串

09/05
0
0
【九度OJ1522】|【剑指offer21】包含min函数的栈

aqia358
2013/12/23
0
0
【九度OJ1348】|【剑指offer36】数组中的逆序对

aqia358
2014/03/17
0
0
*【九度OJ1362】|【剑指offer20】顺时针打印矩阵

aqia358
2013/12/25
0
0
[算法总结] 3 道题搞定 BAT 面试——堆栈和队列

09/04
0
0

PHP生成CSV之内部换行

31分钟前
1
0

34分钟前
4
0

3
0
JAVA-四元数类

public class Quaternion { private final double x0, x1, x2, x3; // 四元数构造函数 public Quaternion(double x0, double x1, double x2, double x3) { this.x0 = ......

Pulsar-V

17
0
Xshell利用Xftp传输文件,使用pure-ftpd搭建ftp服务

Xftp传输文件 如果已经通过Xshell登录到服务器，此时可以使用快捷键ctrl+alt+f 打开Xftp并展示Xshell当前的目录，之后直接拖拽传输文件即可。 pure-ftpd搭建ftp服务 pure-ftpd要比vsftp简单，...

2
0