LanQiaoTestCodes/查找两个总和为特定值的索引.java

36 lines
827 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package LanQiaoOJ;
/*题目描述
给定一个数组,找到两个总和为特定值的索引。
例如给定数组 [1, 2, 3, -2, 5, 7],给定总和 7则返回索引 [1, 4]。
若有多组符合情况则输出索引对中小索引最小的一组。
输入描述
第一行为给定数组的长度,不超过 100。
第二行为数组元素,元素大小不超过 100可能为负数
第三行为特定值。
输出描述
输出一行,为两个索引值,升序输出。*/
import java.util.Scanner;
public class 查找两个总和为特定值的索引 {
static int N=110;
static int [] a=new int[N];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
int target=sc.nextInt();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++) {
if(a[i]+a[j]==target) {
System.out.println(i+" "+j);
return;
}
}
}
}