汽車加油問題 java版 演算法設計與分析
阿新 • • 發佈:2019-02-13
public static void main(String[] args) throws IOException
{
int n,k;
int count=0;//加油次數
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入汽車加滿油後可行駛路程:");
String str=br.readLine();
n=Integer.parseInt(str);
System.out.println("請輸入途經加油站總數:");
String str_k=br.readLine();
k=Integer.parseInt(str_k);
int distance[]=new int[k+1];//加油站間距
int note[] = new int[k];//記錄加油站點
for(int i=0;i<=k;i++)
{
System.out.println("請輸入加油站"+(i)+"到加油站"+(i+1)+"間距");
String temp=br.readLine();
distance[i]=Integer.parseInt(temp.toString());
}
Problem p=new Problem();
count=p.Greedy(distance, note,n);
if (count >= 0)
{
if (count == 0)
System.out.println("汽車不用加油就可到達終點!");
else
{
System.out.println("汽車在旅途中需加"+count+"次油!");
System.out.println("分別在以下加油站加了油:");
for (int i = 0; i < note.length; i++)
{
if (note[i] != 0)
{//輸出需加油站點
System.out.println("第" + note[i] + "個加油站!");
}
}
}
}
else
System.out.println("汽車無法到達終點!");
}
{
int n,k;
int count=0;//加油次數
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入汽車加滿油後可行駛路程:");
String str=br.readLine();
n=Integer.parseInt(str);
System.out.println("請輸入途經加油站總數:");
String str_k=br.readLine();
k=Integer.parseInt(str_k);
int distance[]=new int[k+1];//加油站間距
int note[] = new int[k];//記錄加油站點
for(int i=0;i<=k;i++)
{
System.out.println("請輸入加油站"+(i)+"到加油站"+(i+1)+"間距");
String temp=br.readLine();
distance[i]=Integer.parseInt(temp.toString());
}
Problem p=new Problem();
count=p.Greedy(distance, note,n);
if (count >= 0)
{
if (count == 0)
System.out.println("汽車不用加油就可到達終點!");
else
{
System.out.println("汽車在旅途中需加"+count+"次油!");
System.out.println("分別在以下加油站加了油:");
for (int i = 0; i < note.length; i++)
{
if (note[i] != 0)
{//輸出需加油站點
System.out.println("第" + note[i] + "個加油站!");
}
}
}
}
else
System.out.println("汽車無法到達終點!");
}