输入数据包括五行:
第一行为周围出租车打车点的个数n(1 ≤ n ≤ 50)
第二行为每个出租车打车点的横坐标tX[i] (-10000 ≤ tX[i] ≤ 10000)
第三行为每个出租车打车点的纵坐标tY[i] (-10000 ≤ tY[i] ≤ 10000)
第四行为办公室坐标gx,gy(-10000 ≤ gx,gy ≤ 10000),以空格分隔
第五行为走路时间walkTime(1 ≤ walkTime ≤ 1000)和taxiTime(1 ≤ taxiTime ≤ 1000),以空格分隔
输出一个整数表示,小易最快能赶到办公室的时间
2 -2 -2 0 -2 -4 -2 15 3
42
package go.jacob.day913; import java.util.Scanner; public class Demo1 { 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 gx = sc.nextInt(), gy = sc.nextInt(); int walkTime = sc.nextInt(), taxiTime = sc.nextInt(); int min = walkTime * (Math.abs(gx) + Math.abs(gy)); for (int i = 0; i < n; i++) { int tmp = walkTime * (Math.abs(X[i]) + Math.abs(Y[i])) + taxiTime * (Math.abs(gx - X[i]) + Math.abs(gy - Y[i])); if (tmp < min) min = tmp; } System.out.println(min); sc.close(); } }
import java.util.*; //该题目比较简单,针对两种不同的方式计算即可 //1. 完全走步 //2. 先走到停车点,之后坐车到达 public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); //获得出租车打车点 int Tcount = scanner.nextInt(); int [] txArray = new int[Tcount]; int [] tyArray = new int[Tcount]; //初始化出租车打车点的x坐标 for(int i = 0;i < Tcount;i++){ txArray[i] = scanner.nextInt(); } //初始化出租车打车点的Y坐标 for(int j = 0;j < Tcount;j++){ tyArray[j] = scanner.nextInt(); } //得到目的地地址 int gx = scanner.nextInt(); int gy = scanner.nextInt(); //得到走路时间和打车时间 int walkTime = scanner.nextInt(); int taxiTime = scanner.nextInt(); //完全走路所需要的时间 int totalByWalk = (Math.abs(gx)+ Math.abs(gy)) * walkTime; //保存打车所费时间的最小值 int totalByTaxi = Integer.MAX_VALUE; //对于打车 for(int k = 0;k < Tcount;k++){ //走到停车点的时间 int firstTime = (Math.abs(txArray[k])+Math.abs(tyArray[k])) * walkTime; //坐车到目的地时间 int secondTime = (Math.abs(txArray[k]- gx) + Math.abs(tyArray[k]- gy)) * taxiTime; totalByTaxi = Math.min(totalByTaxi,firstTime + secondTime); } System.out.println(Math.min(totalByWalk,totalByTaxi)); } }
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int tx[] = new int[n]; int ty[] = new int[n]; for (int i = 0; i < n; i++) { tx[i] = sc.nextInt();//打车点x坐标 } for (int i = 0; i < n; i++) { ty[i] = sc.nextInt();//打车点y坐标 } int gx = sc.nextInt();//公司x坐标 int gy = sc.nextInt();//公司y坐标 int wt = sc.nextInt();// 走路速度 int dt = sc.nextInt();// 打车速度 int walkTime = (Math.abs(gx) + Math.abs(gy)) * wt;// 如果全部走路 int driveTime = Integer.MAX_VALUE; for (int i = 0; i < n; i++) {// 如果打车 driveTime = Math.min(driveTime, (Math.abs(ty[i]) + Math.abs(tx[i])) * wt + (Math.abs(ty[i]-gy)+Math.abs(tx[i]-gx)) * dt); } System.out.println(Math.min(driveTime, walkTime)); } }
#include<iostream>#include<vector>#include<map>#include<algorithm>#include<string>#include<cmath>using namespace std;intWal, Tex;intgetT(intx, inty, intrx, intry){// 计算走到车站的时间intwa = (abs(x)+abs(y))*Wal;// 计算车站到公司的时间intte = (abs(rx-x) + abs(ry-y))*Tex;// 返回总花费时间returnwa+te;}// 主函数intmain(){intn;inttx[51];intty[51];intcom[2];intwal, tex;cin >> n;inti=0;while(i!=n){cin>>tx[i++];}i=0;while(i!=n){cin>>ty[i++];}cin>>com[0]>>com[1];cin>>wal>>tex;Wal = wal;Tex = tex;// 直接步行的时间intwalktime = (abs(com[0]) + abs(com[1]))*wal;// 循环处理从每一个车站乘车去公司的时间intt = 2147483647;for(inti=0; i < n; i++){// 计算每一个车站乘车的总时间inttemp = getT(tx[i], ty[i], com[0], com[1]);// 判断是否是最小时间if(temp < t){t = temp;}}cout<<(t<walktime?t:walktime)<<endl;}
#include <iostream> using namespace std; struct Point { int x; int y; } point; int main() { int n; cin >> n; Point taxi[n]; for ( int i = 0; i < n; i++ ) cin >> taxi[i].x >> taxi[i].y; Point company; cin >> company.x >> company.y; int walkTime, taxiTime; cin >> walkTime >> taxiTime; int res = ( abs(company.x) + abs(company.y) ) * walkTime; int curWalk, curTaxi; for ( int i = 0; i < n; i++ ) { curWalk = ( abs(taxi[i].x) + abs(taxi[i].y) ) * walkTime; curTaxi = ( abs(taxi[i].x - company.x) + abs(taxi[i].y - company.y) ) * taxiTime; res = min(res, (curWalk + curTaxi) ); } cout << res << endl; return 0; }
const timeCal = (posX, posY, offX, offY, speed) => { const yTime = Math.abs(offY - posY) * speed const xTime = Math.abs(offX - posX) * speed return yTime + xTime } const main = (taxiNum, taxiX, taxiY, officeX, officeY, walkSpeed, taxiSpeed) => { var time = timeCal(0, 0, officeX, officeY, walkSpeed); for(let i = 0; i < taxiNum; i++){ let walkTime = timeCal(0, 0, taxiX[i], taxiY[i], walkSpeed) let taxiTime = timeCal(taxiX[i], taxiY[i], officeX, officeY, taxiSpeed) let totalTime = walkTime + taxiTime time = (totalTime < time)?totalTime:time } return time } //except: 42 console.log(main(2, [-2, 0],[-2, -2], -4, -2, 15, 3))
#该题目比较简单,针对两种不同的方式计算即可 #完全走步 #先走到停车点,之后坐车到达,遍历所有停车点,不要觉得穷举就不能做 #有时候最直接的思路能解决就行 class solution(): def timeToCompany(self,n,tx,ty,gx,gy,walkTime,taxiTime): n = int(n) tx = [int(item) for item in tx] ty = [int(item) for item in ty] gx = int(gx) gy = int(gy) walkTime = int(walkTime) taxiTime = int(taxiTime) #完全走路需要的时间如下: totalTimeWalk = (abs(gx)+abs(gy))*walkTime #遍历所有打车点,求出最小的打车时间 totalTimeTaxi = float("inf")#保存打车所费时间的最小值 for i in range(n): #走到停车点的时间 firstTime = (abs(tx[i])+abs(ty[i]))*walkTime #坐车到公司的时间 secondTime = (abs(tx[i]-gx)+abs(ty[i]-gy))*taxiTime totalTimeTaxi = min(totalTimeTaxi,firstTime+secondTime) print(min(totalTimeTaxi,totalTimeWalk)) if __name__ == "__main__": n = input() tx = input().split(' ') ty = input().split(' ') gx,gy = input().split(' ') walkTime,taxiTime = input().split(' ') solution().timeToCompany(n,tx,ty,gx,gy,walkTime,taxiTime)
# 将步行,步行到打车点再打车的花费时间全部列举出来,找到最小值即可 import sys class Solution: def get_arrival_time(self, arr_x, arr_y, aim, walk_speed, taxi_speed): cost = self.get_walk_time(aim, walk_speed) index = 0 while index < len(arr_x): tmp_cost = self.get_taxi_time(aim, arr_x[index], arr_y[index], walk_speed, taxi_speed) cost = min([cost, tmp_cost]) index += 1 print(cost) def get_walk_time(self, aim, walk_speed): total_dis = abs(aim[0]) + abs(aim[1]) return total_dis * walk_speed def get_taxi_time(self, aim, x, y, walk_speed, taxi_speed): walk_time = self.get_walk_time((x, y), walk_speed) taxi_dis = abs(aim[0]-x) + abs(aim[1]-y) return walk_time + taxi_dis*taxi_speed if __name__ == '__main__': n = int(sys.stdin.readline()) arr_x = list(map(int, sys.stdin.readline().split())) arr_y = list(map(int, sys.stdin.readline().split())) aim = list(map(int, sys.stdin.readline().split())) w_speed, t_speed = map(int, sys.stdin.readline().split()) solution = Solution() solution.get_arrival_time(arr_x, arr_y, aim, w_speed, t_speed)
// // main.cpp // violent_enumeration // // Created by LU QI on 2017/11/06. // Copyright © 2017 LU QI. All rights reserved. // #include <iostream> #include <vector> #include <string> using namespace std; int spend(int x1,int y1,int x2,int y2,int Time){ int spend = abs(x1-x2)+abs(y1-y2); spend *= Time; return spend; } int main(int argc, const char * argv[]) { int n,gx,gy,walkTime,taxiTime,t1,t2,t; cin>>n; int tx[n],ty[n]; for(int i=0;i<n;i++){ cin>>tx[i]; } for(int i=0;i<n;i++){ cin>>ty[i]; } cin>>gx>>gy>>walkTime>>taxiTime; t = spend(tx[0], ty[0], 0, 0, walkTime)+spend(tx[0],ty[0],gx,gy,taxiTime); for(int i=1;i<n;i++){ t1 = spend(tx[i],ty[i],0,0,walkTime); t2 = spend(tx[i],ty[i],gx,gy,taxiTime); if(t>t1+t2){ t = t1+t2; } } int f = spend(0,0,gx,gy,walkTime); t = t>f?f:t; cout<<t<<endl; return 0; }
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
int n;
cin>>n;
int tx[n], ty[n];
for (int i = 0; i < n; i++) {
cin>>tx[i];
}
for (int i = 0; i < n; i++) {
cin>>ty[i];
}
int gx, gy;
cin>>gx>>gy;
int walkT, taxiT;
cin>>walkT>>taxiT;
int totalWalkT = (abs(gx) + abs(gy)) * walkT; //完全走去的时间
int chosenTaxiX = 0, chosenTaxiY = 0, minLenToTaxi = 20000;
for (int i = 0; i < n; i++) {
int len = abs(tx[i]) + abs(ty[i]);
if (len < minLenToTaxi) {
minLenToTaxi = len;
chosenTaxiX = tx[i];
chosenTaxiY = ty[i];
}
}
int lenToG = abs(gx - chosenTaxiX) + abs(gy - chosenTaxiY);
int totalTaxiT = minLenToTaxi * walkT + lenToG * taxiT; //打车去的时间
cout<<(totalWalkT > totalTaxiT ? totalTaxiT : totalWalkT);
return 0;
}
package mem; import java.util.*; public class GoComp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int taxNum = sc.nextInt(); int taxX[] = new int[taxNum]; int taxY[] = new int[taxNum]; for (int i = 0; i < taxNum; i++) { taxX[i] = sc.nextInt(); } for (int i = 0; i < taxNum; i++) { taxY[i] = sc.nextInt(); } int gx = sc.nextInt(); int gy = sc.nextInt(); int walkTime = sc.nextInt(); int taxTime = sc.nextInt(); int timeWalk = (Math.abs(gx) + Math.abs(gy)) * walkTime; int timeTax = timeWalk; for (int i = 0; i < taxNum; i++) { timeTax = Math.min(timeTax, ((Math.abs(taxX[i]) + Math.abs(taxY[i])) * walkTime + (Math.abs(gx - taxX[i]) + Math.abs(gx - taxX[i])) * taxTime)); } System.out.println(timeTax); } }
#include <iostream> #include <vector> #include <cmath> using namespace std; int main(){ int taxi_num = 0; cin >> taxi_num; vector<int> tx; vector<int> ty; int i = 0,j = 0; for(int i = 0;i < taxi_num;i++){ int x; cin >> x; tx.push_back(x); } for(int i = 0;i < taxi_num;i++){ int y; cin >> y; ty.push_back(y); } int fx,fy; cin>>fx>>fy; int wt,tt; cin>>wt>>tt; int min_time = 0x7FFFFFFF; int sum = 0,nx,ny,tl,time2; for(int i = 0; i < taxi_num;i++){ sum = abs(tx[i])+ abs(ty[i]); nx = tx[i]; ny = ty[i]; tl = abs(fx-nx) + abs(fy -ny); time2 = sum * wt + tl * tt; //走到最近的出租车点,坐车 if(min_time > time2){ min_time = time2; } } int time1 = wt * (abs(fx) + abs(fy));//只是步行 if(min_time < time1) cout << min_time << endl; else cout << time1 << endl; return 0; }