首页 > 专题系列 > Java解POJ > POJ 1484 Blowing Fuses [解题报告] Java
2013
11-09

POJ 1484 Blowing Fuses [解题报告] Java

Blowing Fuses

问题描述 :

Maybe you are familiar with the following situation. You have plugged in a lot of electrical devices, such as toasters, refrigerators, microwave ovens, computers, stereos, etc, and have them all running. But at the moment when you turn on the TV, the fuse blows, since the power drawn from all the machines is greater than the capacity of the fuse. Of course this is a great safety feature, avoiding that houses burn down too often due to fires ignited by overheating wires. But it is also annoying to walk down to the basement (or some other inconvenient place) to replace to fuse or switch it back on.

What one would like to have is a program that checks before turning on an electrical device whether the combined power drawn by all running devices exceeds the fuses capacity (and it blows), or whether it is safe to turn it on.

输入:

The input consists of several test cases. Each test case describes a set of electrical devices and gives a sequence of turn on/off operations for these devices.

The first line of each test case contains three integers n, m and c, where n is the number of devices (n <= 20), m the number of operations performed on these devices and c is the capacity of the fuse (in Amperes). The following n lines contain one positive integer ci each, the consumption (in Amperes) of the i-th device.

This is followed by m lines also containing one integer each, between 1 and n inclusive. They describe a sequence of turn on/turn off operations performed on the devices. For every number, the state of that particular devices is toggled, i.e. if it is currently running, it is turned off, and if it is currently turned off, it will by switched on. At the beginning all devices are turned off.

The input will be terminated by a test case starting with n = m = c = 0. This test case should not be processed.

输出:

For each test case, first output the number of the test case. Then output whether the fuse was blown during the operation sequence. The fuse will be blown if the sum of the power consumptions ci of turned on devices at some point exceeds the capacity of the fuse c.

If the fuse is not blown, output the maximal power consumption by turned on devices that occurred during the sequence.

Output a blank line after each test case.

样例输入:

2 2 10
5
7
1
2
3 6 10
2
5
7
2
1
2
3
1
3
0 0 0

样例输出:

Sequence 1
Fuse was blown.

Sequence 2
Fuse was not blown.
Maximal power consumption was 9 amperes.

解题代码:

//* @author: SmilingWang
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
 public static void main(String[] args){
  int n, m, c;
  int i, j = 1, k;
  Scanner scanner = new Scanner(System.in);
  boolean blowFuse = false;
  int sumOfPower = 0;
  while(true){
	n = scanner.nextInt();
	m = scanner.nextInt();
	c = scanner.nextInt();
	if(n == 0 && m == 0 && c == 0){
         break;
	}
	sumOfPower = 0;
	int max = 0;
	blowFuse = false;
	Device[] devices = new Device[n + 1];
	for(i = 1; i <= n; i++){
	  int num = scanner.nextInt();
	  devices[i] = new Device(i, num, false);
	}
	for(i = 1; i <= m; i++){
	  k = scanner.nextInt();
	  if(devices[k].state && sumOfPower > 0){
	    sumOfPower -= devices[k].pow;
	   devices[k].state = false;
	}
	else if(!devices[k].state){
	   sumOfPower += devices[k].pow;
	   devices[k].state = true;
	   if(sumOfPower > max){
		max = sumOfPower;
	   }
	   if(sumOfPower > c){
		blowFuse = true;
	   }
	}
    }
    System.out.println("Sequence " + (j++));
    if(blowFuse){
	System.out.println("Fuse was blown.");
    }
    else{
	System.out.println("Fuse was not blown.\nMaximal power consumption was "+ max +" amperes.");
    }
    System.out.println();
  }
		
}
  
  static class Device{
    int num;
    int pow;
   boolean state; // true : power on , false : power in
   public Device(int num, int pow, boolean state){
   this.num = num;
   this.pow = pow;
   this.state = state;
   }

  public boolean equals(Device rhs){
	return(this.num == rhs.num);
 }
}
}

  1. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks