杭電ACM 1008 1009
1008 .Elevator
Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
Output
Print the total time on a single line for each test case.
題目大意:電梯停留要花5秒,上一樓花6秒,下一樓花4秒,給出一個樓層的序列,判斷電梯完成這個序列要花多久。
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int n;
while((n=s.nextInt())!=0 ){
int time=0;
time=n*5;
int prior=0,current;
for(int i=0;i<n;i++){
current=s.nextInt();
if(current>prior)
time=time+(current-prior)*6;
else
time=time+(prior-current)*4;
prior=current;
}
System.out.println(time);
}
}
}
1009 .FatMouse’ Trade
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
題目大意:老鼠用自己的貓糧可以去換每個房間的java豆,每個房間最多可以用多少貓糧換多少java豆在輸入當中,如果貓糧不足了,可以按比例換,求如何選擇換的多。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
class room{
int x,y;
double ratio;
public room(int x,int y){
this.x=x;
this.y=y;
this.ratio=((double)x)/y;
}
}
class SortByRatio implements Comparator<Object>{
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
room r1=(room)o1;
room r2=(room)o2;
if(r1.ratio>r2.ratio)
return -1;
return 1;
}
}
public class Main {
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int M,N;
while((M=s.nextInt())!=-1){
N=s.nextInt();
ArrayList<room> a=new ArrayList<room>();
for(int i=0;i<N;i++){
int x=s.nextInt();
int y=s.nextInt();
a.add(new room(x,y));
}
Collections.sort(a, new SortByRatio());
double J=0;
for(room r:a){
if(M>=r.y){
M=M-r.y;
J=(double)(r.x)+J;
}
else{
J=M*r.ratio+J;
break;
}
}
String result =String.format("%.3f",J);
System.out.println(result);//System.out.printf("%.3f\n",J);報錯
}
}
}
用到ArrayList類。
這裡我用System.out.printf(“%.3f\n”,J);來輸出出現presentation錯誤,java 杭電acm換行輸出最好別用”\n”,用System.out.println();通過了。