杭电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.

http://acm.hdu.edu.cn/showproblem.php?pid=1008

题目大意:电梯停留要花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;iif(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.

http://acm.hdu.edu.cn/showproblem.php?pid=1009

题目大意:老鼠用自己的猫粮可以去换每个房间的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 a=new ArrayList();
            for(int i=0;iint 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();通过了。

你可能感兴趣的:(Acm)