第一行为一个整数n(n ≤ 1000),表示超超一共拥有n个陷阱。 第二行有n个整数xi,表示第i个陷阱的横坐标 第三行有n个整数yi,表示第i个陷阱的纵坐标 保证坐标都在草地范围内。
输出一个整数,表示小易最少可能多少秒就落入超超的陷阱
3 4 6 8 1 2 1
3
//理解题意后直接计算就ok了 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[][] num = new int[n][2]; for(int i=0;i<n;i++){ num[i][0] = sc.nextInt(); } for(int i=0;i<n;i++){ num[i][1] = sc.nextInt(); } int min = num[0][0]-1+num[0][1]-1; int t = 0; for(int i=1;i<n;i++){ t = num[i][0]-1+num[i][1]-1; if(min>t)min=t; } System.out.println(min); } } }
用python 3 一行解决问题
import sys
def save_xiaoyi(list_x, list_y):
result_list = [(a1+a2) for (a1, a2) in zip(list_x, list_y)]
return min(result_list)-2
# 读取输入数据,将每行以列表形式存储
data = sys.stdin.readlines()
# 提取x坐标
list_x = [int(i) for i in data[1].split()]
# 提取y坐标
list_y = [int(i) for i in data[2].split()]
print(save_xiaoyi(list_x, list_y))
#include<math.h> #include<stdio.h> #define min(a,b) a<b?a:b int x[1000],y[1000],n,i,Min=999999999; int main(){ for(scanf("%d",&n),i=0;i<n;i++) scanf("%d",&x[i]); for(i=0;i<n;i++) scanf("%d",&y[i]),Min=min(Min,(int)fabs(x[i]-1)+(int)fabs(y[i]-1)); printf("%d\n",Min); }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String strN; while((strN = br.readLine()) != null){ int n = Integer.parseInt(strN); String[] lineX = br.readLine().split(" "); String[] lineY = br.readLine().split(" "); int minSecond = Integer.MAX_VALUE; // 即求取与小易初始位置最小的曼哈顿距离 for(int i = 0; i < n; i++){ minSecond = Math.min(minSecond, Math.abs(Integer.parseInt(lineX[i]) - 1) + Math.abs(Integer.parseInt(lineY[i]) - 1)); } System.out.println(minSecond); } } }
/*先对每一组(x,y)求和,然后减去初始坐标(1,1)的和2,放入C数组,在对C 进行sort排序,输出最小值即可*/ #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a[1001],b[1001],c[1001]; for(int i=0;i<n;i++) { cin>>a[i]; c[i]=a[i]; } for(int i=0;i<n;i++) { cin>>b[i]; c[i]=c[i]+b[i]-2; } sort(c,c+n); cout<<c[0]<<endl; }
import java.util.Scanner;
//非常简单的一道题,只要用二维数组存储好相应的横纵坐标,再将二维数组的值相加
//-2就可以了 -2的原因是小易的起始坐标为(1,1) 需要减去它
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
//陷阱总数
int count=s.nextInt();
int[][] arrays =new int[count][2];
for(int i=0;i<count;i++)
{
int x=s.nextInt();
arrays[i][0]=x;
}
for(int j=0;j<count;j++)
{
int y=s.nextInt();
arrays[j][1]=y;
}
int min=Integer.MAX_VALUE;
int temp=0;
for(int k=0;k<count;k++)
{
temp=0;
for(int m=0;m<arrays[0].length;m++)
{
temp=temp+arrays[k][m];
}
if(temp<min)
{
min=temp;
}
}
System.out.print(min-2);
}
}
import java.util.Scanner; public class Main{ public static void main(String []args){ Scanner sc=new Scanner(System.in); int num=sc.nextInt(); int minSec=10000; int [] row=new int[num]; int [] column=new int[num]; for(int i=0;i<num;i++){ row[i]=sc.nextInt(); } for(int i=0;i<num;i++){ column[i]=sc.nextInt(); } for(int i=0;i<num;i++){ if((row[i]+column[i]-2)<minSec){ minSec=row[i]+column[i]-2; } } System.out.println(minSec); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input=new Scanner(System.in); while(input.hasNext()){ int n=input.nextInt(); int[] x=new int[n]; int[] y=new int[n]; for(int i=0; i<n; i++) x[i]=input.nextInt(); for(int i=0; i<n; i++) y[i]=input.nextInt(); int min=x[0]+y[0]; for(int i=1; i<n; i++){ int temp=x[i]+y[i]; if(temp<min) min=temp; } System.out.println(min-2); } } }
#include <iostream> #include <cmath> using namespace std; int main() { int n; cin >> n; int minm; int x[n],y[n]; for(int i =0;i<n;i++) cin >> x[i]; for(int i=0;i<n;i++) cin >> y[i]; minm=abs(x[0]-1)+abs(y[0]-1); for(int i=1;i<n;i++) if((abs(x[i]-1)+abs(y[i]-1))<minm) minm=abs(x[i]-1)+abs(y[i]-1); cout << minm << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n,dp[1010][1010],x[1010],y[1010]; while(cin>>n) { int Min = INT_MAX; for(int i=0;i<n;i++) cin>>x[i]; for(int i=0;i<n;i++) { cin>>y[i]; if(x[i]+y[i]-2 < Min) Min = x[i]+y[i]-2; } cout<<Min<<endl; } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int[] xs=new int[n]; int[] ys=new int[n]; for(int i=0;i<n;i++) xs[i]=scanner.nextInt()-1; for(int i=0;i<n;i++) ys[i]=scanner.nextInt()-1; int min=Integer.MAX_VALUE; for(int i=0;i<n;i++){ if(xs[i]+ys[i]<min) min=xs[i]+ys[i]; } System.out.println(min); } }