第一行为一个整数n(n ≤ 1000),表示超超一共拥有n个陷阱。 第二行有n个整数xi,表示第i个陷阱的横坐标 第三行有n个整数yi,表示第i个陷阱的纵坐标 保证坐标都在草地范围内。
输出一个整数,表示小易最少可能多少秒就落入超超的陷阱
3 4 6 8 1 2 1
3
//终于找到一题会做的了 import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[] x = new int[n]; for(int i = 0; i < n; i++){ x[i] = sc.nextInt() - 1; } int min = Integer.MAX_VALUE; for(int i = 0; i < n; i++){ int y = sc.nextInt(); min = Math.min(min, y - 1 + x[i]); } System.out.println(min); } sc.close(); } }
import java.util.Scanner; /** * Created by ZLei on 2018/8/7. */ public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int ans = 1000000; int trapNum = sc.nextInt(); int[] x = new int[trapNum]; int[] y = new int[trapNum]; for(int i = 0; i < trapNum; i++){ x[i] = sc.nextInt(); } for(int i = 0; i < trapNum; i++){ y[i] = sc.nextInt(); ans = Math.min((x[i]+y[i]-2), ans); } System.out.println(ans); } sc.close(); } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); int n = Integer.parseInt(line); line = scanner.nextLine(); String []s = line.split(" "); int [][] trap = new int[n][2]; for (int i = 0;i < n;i ++) { trap[i][0] = Integer.parseInt(s[i]); } line = scanner.nextLine(); s = line.split(" "); for (int i = 0;i < n;i ++) { trap[i][1] = Integer.parseInt(s[i]); } int min = min(trap); System.out.println(min); } public static int min(int[][] trap) { int min = Integer.MAX_VALUE; int minX = trap[0][0],minY = trap[0][1]; int len = minX + minY - 2; for (int i = 0;i < trap.length;i ++) { int x = trap[i][0]; int y = trap[i][1]; len = x + y - 2; if (len < min) { min = len; } } return min; } }
import java.util.*;
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int count = in.nextInt(); int[] datax = new int[count]; int[] datay = new int[count]; int sum = Integer.MAX_VALUE; for (int i = 0; i < count; i++) datax[i] = in.nextInt(); for (int i = 0; i < count; i++) datay[i] = in.nextInt(); for (int i = 0; i < count; i++) if (datax[i] + datay[i] < sum) sum = datax[i] + datay[i]; System.out.println(sum - 2); } } }
import java.util.Scanner; public class Main { public static void main( String[] args ) { Scanner scan = new Scanner(System.in); //获取数组长度 int num = scan.nextInt(); //因为只计算到(1,1)的距离,只有一个数组就足够 int[] a = new int[num]; int i = 0; //获取所有横坐标 while(scan.hasNext()){ a[i++] = scan.nextInt(); if(i==a.length) { break; } } //获取纵坐标并计算长度,比较最小值 int min = Integer.MAX_VALUE; i = 0; while(scan.hasNext()){ a[i] += scan.nextInt(); if(a[i]<min){ min = a[i]; } i++; if(i==a.length) { break; } } //输出距离-2 System.out.println(min-2); } }
解救小易可以简单理解为从左上角到所有点中步数最小值,因为题中说明了每次都只能横走一步或者纵走一步,所以只需要将要到达的点的横纵坐标进行相加然后减去原始位置1+1即表示到达该点所需要走的步数
public static void main(String[] args){
int n;
int a[] = new int[1000];
int b[] = new int[1000];
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(int i=1; i<=n; i++){
a[i] = in.nextInt();
}
for(int i=1; i<=n; i++){
b[i] = in.nextInt();
}
int dis = 2000;
for(int i=1; i<=n; i++){
if(dis > a[i]+b[i])
dis = a[i] + b[i];
}
System.out.print(dis-2);
}
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for(int i=0;i<n;i++){ a[i]=scanner.nextInt()-1; } for(int i=0;i<n;i++){ a[i]=scanner.nextInt()-1+a[i]; } scanner.close(); Arrays.sort(a); System.out.println(a[0]); } }
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] x=new int[n]; int[] y=new int[n]; for(int i=0;i<n;i++){ x[i]=sc.nextInt(); } for(int i=0;i<n;i++){ y[i]=sc.nextInt(); } int[] sum=new int[n]; for(int i=0;i<n;i++){ sum[i]=x[i]+y[i]-2; } Arrays.sort(sum); System.out.println(sum[0]); } } 难道就这样简单吗?反正是通过了。