import java.util.Scanner;
public class Test
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int n,cur=0,tmp,sum;
while((n=in.nextInt())!=0)
{
sum = 0;
cur = 0;
while(n--!=0)
{
tmp = in.nextInt();
if(tmp>cur)
{
sum+=(tmp-cur)*6+5;
}
else if(tmp<cur)
{
sum+=(cur-tmp)*4+5;
}
else
sum+=5;
cur = tmp;
}
System.out.println(sum);

}

}
}

解决方案 »

  1.   


    import java.util.Scanner;
    public class Test
    {
    public static void main(String[]args)
    {
    Scanner in = new Scanner(System.in);
    int n,cur=0,tmp,sum;
    while((n=in.nextInt())!=0)
    {
    sum = 0;
    cur = 0;
    while(n--!=0)
    {
    tmp = in.nextInt();
    if(tmp>cur)
    {
    sum+=(tmp-cur)*6+5;
    }
    else if(tmp<cur)
    {
    sum+=(cur-tmp)*4+5;
    }
    else
    sum+=5;
    cur = tmp;
    }
    System.out.println(sum);

    }

    }
    }
      

  2.   

    不好意思  贴错了 重贴下。。import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Scanner;
    public class Main
    {

    public static void main(String[]args)
    {
    Scanner in = new Scanner(System.in);
    int m,n;
    int []j = new int[1001];
    int []p = new int[1001];
    P []a =new P[1001];
    for(int i=0;i<1001;i++)
    a[i]=new P();

    m = in.nextInt();
    n = in.nextInt();

    double sum = 0.0 ;
    int ret = 0;
    while(!(m==-1 && n==-1))
    {
    ret=0;
    sum=0.0;
    ret = 0;
    for(int i=0;i<n-ret;i++)
    {
    j[i]=in.nextInt();
    p[i]=in.nextInt();
    if(p[i]==0)
    {
    sum+=j[i];
    ret++;
    i--;
    continue;
    }
    a[i].q=(double)j[i]/p[i];
    a[i].pos=i;
    }
         

      Arrays.sort(a,new Cmp() );
       int pos = 0;
      
      
       while(pos<n-ret)
       {

       if(m>=p[a[pos].pos])
       {
       sum+=j[a[pos].pos];
       m-=p[a[pos].pos];
       pos++;

       }
       else 
       {
       sum+=a[pos].q*m;
       pos++;    
       break;
       }
       }
       BigDecimal   b   =   new   BigDecimal(sum);
       System.out.println(b.setScale(3,BigDecimal.ROUND_HALF_DOWN).doubleValue());

    m = in.nextInt();
    n = in.nextInt();

    }
    }
    }

    class P
    {
    int pos;
    double q;

    }
    class Cmp implements Comparator<P>
    { @Override
    public int compare(P a, P b) {
    return a.q>b.q?-1:1;
    }
    }
      

  3.   

    题目 
    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.
    Codeimport java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Scanner;
    public class Main
    {

    public static void main(String[]args)
    {
    Scanner in = new Scanner(System.in);
    int m,n;
    int []j = new int[1001];
    int []p = new int[1001];
    P []a =new P[1001];
    for(int i=0;i<1001;i++)
    a[i]=new P();

    m = in.nextInt();
    n = in.nextInt();

    double sum = 0.0 ;
    int ret = 0;
    while(!(m==-1 && n==-1))
    {
    ret=0;
    sum=0.0;
    ret = 0;
    for(int i=0;i<n-ret;i++)
    {
    j[i]=in.nextInt();
    p[i]=in.nextInt();
    if(p[i]==0)
    {
    sum+=j[i];
    ret++;
    i--;
    continue;
    }
    a[i].q=(double)j[i]/p[i];
    a[i].pos=i;
    }
         

      Arrays.sort(a,new Cmp() );
       int pos = 0;
      
      
       while(pos<n-ret)
       {

       if(m>=p[a[pos].pos])
       {
       sum+=j[a[pos].pos];
       m-=p[a[pos].pos];
       pos++;

       }
       else 
       {
       sum+=a[pos].q*m;
       pos++;    
       break;
       }
       }
       BigDecimal   b   =   new   BigDecimal(sum);
       System.out.println(b.setScale(3,BigDecimal.ROUND_HALF_DOWN).doubleValue());

    m = in.nextInt();
    n = in.nextInt();

    }
    }
    }

    class P
    {
    int pos;
    double q;

    }
    class Cmp implements Comparator<P>
    { @Override
    public int compare(P a, P b) {
    return a.q>b.q?-1:1;
    }
    }
      

  4.   

    这是OJ http://acm.hdu.edu.cn/showproblem.php?pid=1009  我也找不到 总是WA
      

  5.   

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

  6.   


    import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Scanner;
    public class Main
    {

    public static void main(String[]args)
    {
    Scanner in = new Scanner(System.in);
    int m,n,j,p;
    P []a =new P[1001];
    for(int i=0;i<1001;i++)
    a[i]=new P();

    m = in.nextInt();
    n = in.nextInt();

    double sum = 0.0 ;
    while(!(m==-1 && n==-1))
    {
    sum=0.0; for(int i=0;i<n;i++)
    {
    j=in.nextInt();
    p=in.nextInt();
    a[i].q=(double)j/p;
    a[i].j=j;
    a[i].p=p;
    }
         
    P tmp = new P();


    for(int i=0;i<n;i++)
    for(int k=i+1;k<n;k++)
    {
    if(a[i].q<a[k].q)
    {
    tmp.j=a[i].j;
            tmp.p=a[i].p;
    tmp.q=a[i].q;

    a[i].j=a[k].j;
    a[i].p=a[k].p;
    a[i].q=a[k].q;

    a[k].j=tmp.j;
    a[k].p=tmp.p;
    a[k].q=tmp.q;
    }
    }   for(int pos=0;pos<n;pos++)
       {

       if(m>=a[pos].p)
       {
       sum+=a[pos].j;
       m-=a[pos].p;
       }
       else 
       {
       sum+=a[pos].q*m;
       m=0;
       break;
       }
       }
     System.out.println(String.format("%.3f",sum));

    m = in.nextInt();
    n = in.nextInt();


    }
    }
    }

    class P
    {
    int p,j;
    double q;

    }