首页 > 专题系列 > Java解POJ > POJ 3427 Ecology tax [解题报告] Java
2013
11-12

POJ 3427 Ecology tax [解题报告] Java

Ecology tax

问题描述 :

In a big and rich on natural resources country, the government started a campaign to control deforestation. In fact the government is not too interested in how many trees get fallen, but rather how effectively the wood is utilized. So a law was passed which requires every logging company to pay amount of money in proportion to amount of wood that it wastes during operation.

A felling quota on some territory was allotted to a company in this country. Company lorries may only transport logs of exactly L meters long. So when a tree gets sawed into logs, the remainder is wasted.

Trees in this country grow exactly 1 meter per year, so the company may decrease the amount of tax to be paid by simply waiting for some years. Your task is to determine the number of years needed to achieve smallest possible tax. If there is more than one answer, find minimal (earliest) one.

输入:

Input file contains number of trees N, length of log L, followed by integers i1 i2iN — heights of each tree.

Constraints

1 ≤ N ≤ 30000, 1 ≤ L, ik ≤ 30000

输出:

Output file must contain single integer — number of years to wait.

样例输入:

Sample Input 1
3 1 
10 15 11
Sample Input 2
3 2
5 3 6

样例输出:

Sample Output 1
0
Sample Output 2
1

温馨提示:

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

解题代码:

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{
 
 public static void main(String args[])
{
 Scanner sc=new Scanner(System.in);
 int n,L,v;
 n=sc.nextInt();
 L=sc.nextInt();
 int m=0;
 while((n--)!=0)
 {
     v=sc.nextInt();
     if(v%L==0)continue;
     int u=L-v%L;
     if(u>m)m=u;
  }
  System.out.printf("%d",m);
 }
}

  1. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。