[java]代码库
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int i;
Truck tr = new Truck();
for(i = 0; i < n; i++) {
int number = scan.nextInt();
int weight = scan.nextInt();
if(number == 1) {
Appliance app = new TV(weight);
tr.add(app);
}
else if(number == 2) {
Appliance app = new WashMachine(weight);
tr.add(app);
}
else {
Appliance app = new AirConditioner(weight);
tr.add(app);
}
}
System.out.println(tr.getTWeight());
scan.close();
}
}
class Appliance{
private int weight;
public Appliance(int wei) {
weight = wei;
}
public int getWeight(){
return weight;
}
}
class TV extends Appliance{
public TV(int wei) {
super(wei);
}
public int getWeight(){
return super.getWeight();
}
}
class WashMachine extends Appliance{
public WashMachine(int wei) {
super(wei);
}
public int getWeight() {
return super.getWeight();
}
}
class AirConditioner extends Appliance{
public AirConditioner(int wei) {
super(wei);
}
public int getWeight() {
return super.getWeight();
}
}
class Truck{
private static int sum = 0;
public Truck() {
}
public void add(Appliance a) {
sum += a.getWeight();
}
public int getTWeight() {
return sum;
}
}