Java OJジョブ4

18537 ワード

140-家電
Description
               ,     、   、     。                 。
     Appliance(  )         TV、WashMachine AirConditioner,         。
   Truck ,            ,     (      )  。
Main         Truck          。

Input
    
           

  :        :TV:1  WashMachine:2  AirConditioner:3

Output
   

Sample Input
5
1 20
2 30
3 25
3 30
2 40

Sample Output
145

MyAnswer
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Truck truck = new Truck();
        truck.getScan();
        System.out.println(truck.getSumWeight());
    }

}

interface Appliance{
    public int getWeight();
}

class App implements Appliance{
    int weight;
    public int getWeight(){
        return 0;
    }
}

class TV extends App implements Appliance{
    TV(int w) {
        weight=w;
    }
    @Override
    public int getWeight() {
        return weight;
    }
}

class WashMachine extends App implements Appliance{
    WashMachine(int w) {
        weight=w;
    }
    @Override
    public int getWeight() {
        return weight;
    }
}

class AirConditioner extends App implements Appliance{
    AirConditioner(int w) {
        weight=w;
    }
    @Override
    public int getWeight() {
        return weight;
    }
}

class Truck{
    int num;
    App[] app;
    int SumWeight;
    public void getScan(){
        Scanner scan = new Scanner(System.in);
        num = scan.nextInt();
        app = new App[num];
        for(int i=0; i

150-教師クラス
Description
       Teacher,  :
     (int no)、  (String name)、  (int age)、    (String seminary),          get set  。
 Teacher   equals  ,  :        no     true。
  Teacher  toString  ,         “no: **, name:**, age: **, seminary: **”      。

Input
         ,  ,  ,  

Output
     
        

Sample Input
1 Linda 38 SoftwareEngineering
2 Mindy 27 ComputerScience

Sample Output
no: 1, name:Linda, age: 38, seminary: SoftwareEngineering
no: 2, name:Mindy, age: 27, seminary: ComputerScience
false

MyAnswer
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n1 = scan.nextInt();
        String na1 = scan.next();
        int a1 = scan.nextInt();
        String s1 = scan.next();

        Teacher t1 = new Teacher(n1,na1,a1,s1);

        int n2 = scan.nextInt();
        String na2 = scan.next();
        int a2 = scan.nextInt();
        String s2 = scan.next();
        Teacher t2 = new Teacher(n2,na2,a2,s2);

        System.out.println(t1.toString());
        System.out.println(t2.toString());
        System.out.println(t1.equals(t2));

    }

}

class Teacher{
    int no;
    String name;
    int age;
    String seminary;

    Teacher(int no, String name, int age, String seminary){
        setNo(no);
        setName(name);
        setSeminary(seminary);
        setAge(age);
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setSeminary(String seminary) {
        this.seminary = seminary;
    }

    public String getName() {
        return name;
    }

    public int getNo() {
        return no;
    }

    public int getAge() {
        return age;
    }

    public String getSeminary() {
        return seminary;
    }
    public boolean equals(Object o){
        if(o == null)
            return false;
        else{
            boolean res = false;
            if(o instanceof Teacher){
                Teacher t = (Teacher)o;
                if(this.no == t.no){
                    res = true;
                }
            }
            return res;
        }
    }

    public String toString(){
        return "no: "+getNo()+", name:"+getName()+", age: "+getAge()+", seminary: "+getSeminary();
    }

}

149-教師クラス-2
Description
    143
1.      ,     Teacher            (        ),  main     Arrays.sort(Object[] a)    
2.      TeacherManagement,      ,    add(Teacher[]),        ,      search,             ,                           ,     :“no: **, name:**, age: **, seminary: **”。           ,   “no such teacher”。

Input
    
    
        
        

Output
      
          
          

Sample Input
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27

Sample Output
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
no: 3, name: Linda, age: 38, seminary: SoftwareEngineering
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by name:
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by age:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience

MyAnswer
import java.util.*;
import java.lang.reflect.Array;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i;
        int num = scan.nextInt();
        TeacherManagement tm = new TeacherManagement(num);
        Teacher[] t = new Teacher[num];
        for(i=0; i{
    int no;
    String name;
    int age;
    String seminary;

    Teacher(int no, String name, int age, String seminary){
        setNo(no);
        setName(name);
        setSeminary(seminary);
        setAge(age);
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setSeminary(String seminary) {
        this.seminary = seminary;
    }

    public String getName() {
        return name;
    }

    public int getNo() {
        return no;
    }

    public int getAge() {
        return age;
    }

    public String getSeminary() {
        return seminary;
    }
    public boolean equals(Object o){
        if(o == null)
            return false;
        else{
            boolean res = false;
            if(o instanceof Teacher){
                Teacher t = (Teacher)o;
                if(this.no == t.no){
                    res = true;
                }
            }
            return res;
        }
    }

    public String toString(){
        return "no: "+getNo()+", name: "+getName()+", age: "+getAge()+", seminary: "+getSeminary();
    }

    public int compareTo(Teacher t){
        if(this.no == t.no)
            return 0;
        else if(this.no>t.no)
            return 1;
        else
            return -1;
    }
}

class TeacherManagement{
    Teacher[] t;
    int num;

    TeacherManagement(int num){
        this.num = num;
        this.t = new Teacher[num];
    }
    public void add(Teacher[] t){
        this.t = t;
    }
    //   
    public void search(int age){
        int flag = 0;
        System.out.println("search by age:");
        for (Teacher aT : t) {
            if (aT.age == age) {
                flag = 1;
                System.out.println(aT.toString());
            }
        }
        if(flag==0){
            System.out.println("no such teacher");
        }
    }
    public void search(String name){
        int flag = 0;
        System.out.println("search by name:");
        for (Teacher aT : t) {
            if (aT.name.equals(name)) {
                flag = 1;
                System.out.println(aT.toString());
            }
        }
        if(flag==0){
            System.out.println("no such teacher");
        }
    }


}

142-コンピュータクラス
Description
      ,         :   、  、  、   、     ,        (   ),
   ,      (  )    (  )、      (  )、          (GB   )。
                     ,             (       )、         。
      equals  ,                     。      toString  ,          。
 main   

Input
       ,  
CPU:  、  、  
  :  
  :  
   :  
  :  

Output
        
         

Sample Input
Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048
Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048

Sample Output
true
Computer1:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048
Computer2:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048

HINT
                ,    toString equals  
             equals toString  
   
String.format("%.1f",1.234)

MyAnswer
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String cModel = scan.next();
        String cFrequency = scan.next();
        int cNumofCores = scan.nextInt();

        String mModel = scan.next();

        String meModel = scan.next();
        int meSize = scan.nextInt();

        String sModel = scan.next();
        int sSize = scan.nextInt();

        String hModel = scan.next();
        int hSize = scan.nextInt();

        CPU cpu1 = new CPU(cModel,cFrequency,cNumofCores);
        MainBoard mb1 = new MainBoard(mModel);
        Memory mem1 = new Memory(meModel,meSize);
        Screen sc1 = new Screen(sModel,sSize);
        Harddisk hd1 = new Harddisk(hModel,hSize);

        Computer cp1 = new Computer(cpu1,mb1,mem1,sc1,hd1);

        cModel = scan.next();
        cFrequency = scan.next();
        cNumofCores = scan.nextInt();

        mModel = scan.next();

        meModel = scan.next();
        meSize = scan.nextInt();

        sModel = scan.next();
        sSize = scan.nextInt();

        hModel = scan.next();
        hSize = scan.nextInt();

        CPU cpu2 = new CPU(cModel,cFrequency,cNumofCores);
        MainBoard mb2 = new MainBoard(mModel);
        Memory mem2 = new Memory(meModel,meSize);
        Screen sc2 = new Screen(sModel,sSize);
        Harddisk hd2 = new Harddisk(hModel,hSize);

        Computer cp2 = new Computer(cpu2,mb2,mem2,sc2,hd2);

        System.out.println(cp1.equals(cp2));
        System.out.println("Computer1:");
        System.out.println(cp1);
        System.out.println("Computer2:");
        System.out.println(cp2);

    }
}

class Father{
    String model;
    Father(String model){
        this.model = model;
    }
}

class CPU extends Father{
    String freq;
    int numCores;
    public CPU(String model, String freq,int numCores){
        super(model);
        this.freq = freq;
        this.numCores = numCores;
    }
    public boolean equals(Object o){
        if(o==null) return false;
        else if(o instanceof CPU){
            CPU c = (CPU) o;
            return this.model.equals(c.model) && this.freq.equals(c.freq) && this.numCores == c.numCores;
        }
        else
            return false;
    }

    public String toString(){
        return "CPU:
Model: " + model + "
Frequency: " +freq +"
Number of Cores: " + numCores + "
"; } } class MainBoard extends Father{ MainBoard(String model){ super(model); } public boolean equals(Object o){ if(o==null) return false; else if(o instanceof MainBoard){ MainBoard mb = (MainBoard) o; return this.model.equals(mb.model); } else return false; } public String toString(){ return "Mainboard:
Model: "+model+"
"; } } class Memory extends Father{ int size; Memory(String model, int size){ super(model); this.size = size; } public boolean equals(Object o){ if(o==null) return false; else if(o instanceof Memory){ Memory m = (Memory) o; return this.model.equals(m.model) && this.size == m.size; } else return false; } public String toString(){ return "Memory:
Model: "+model+"
Size: "+size+"
"; } } class Screen extends Father{ int size; Screen(String model, int size){ super(model); this.size = size; } public boolean equals(Object o){ if(o==null) return false; else if(o instanceof Screen){ Screen s = (Screen) o; return this.model.equals(s.model) && this.size == s.size; } else return false; } public String toString(){ return "Screen:
Model: "+model+ "
Size: "+size+"
"; } } class Harddisk extends Father{ int size; Harddisk(String model, int size){ super(model); this.size = size; } public boolean equals(Object o){ if(o==null) return false; else if(o instanceof Harddisk){ Harddisk h = (Harddisk) o; return this.model.equals(h.model) && this.size == h.size; } else return false; } public String toString(){ return "Harddisk:
Model: "+model+ "
Size: "+size; } } class Computer{ CPU cpu; MainBoard mainboard; Memory memory; Screen screen; Harddisk harddisk; Computer(CPU cpu,MainBoard mainboard,Memory memory,Screen screen,Harddisk harddisk){ this.cpu = cpu; this.mainboard = mainboard; this.memory = memory; this.screen = screen; this.harddisk = harddisk; } public boolean equals(Object o){ if(o==null) return false; else if(o instanceof Computer){ Computer c = (Computer) o; return this.cpu.equals(c.cpu) && this.mainboard.equals(c.mainboard) && this.memory.equals(c.memory) && this.screen.equals(c.screen) && this.harddisk.equals(c.harddisk); } else return false; } public String toString() { return cpu.toString() + mainboard.toString() + memory.toString() + screen.toString() + harddisk.toString(); } }

139-整数配列比較
Description
        A B, A      B ,          。  B        ,        
           ,   A   B     ,  B      ,  B       。

Input
  A   
  A  

Output
A  B   
A  B   
A  B   

Sample Input
10
23 1 32 87 65 12 21 9 76 45

Sample Output
4
1
5

HINT
  Arrays.sort  

MyAnswer
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        int[] a = new int[num];
        int i;
        int bigger=0,smaller=0,equal=0;

        for(i=0; i b[i])
                bigger++;
            else if(a[i]==b[i])
                equal++;
            else
                smaller++;
        }
        System.out.println(bigger);
        System.out.println(equal);
        System.out.println(smaller);
    }
}


151-マトリックスクラス
Description
      (double[])       :Matrix。        :
(1)set(int row, int col, double value):  row  col       value;
(2)get(int row,int col):  row  col    ;
(3)width():       ;
(4)height():       ;
(5)Matrix add(Matrix b):         b      ;
(6)Matrix multiply(Matrix b):         b      。
(7)Matrix transpose():           ;
(8)toString():              。

Input
      
     
       、   
       、 
         
       
         
       

Output

Sample Input
3 3
1 2 3
4 5 6
7 8 9
2 3 8
1 3
3 3
1 2 3
4 5 6
7 8 9
3 2
1 2
1 2
1 2

Sample Output
row:3 column:3
after set value:
1 2 3
4 5 8
7 8 9
value on (1,3):3
after add:
2 4 6
8 10 14
14 16 18
after multiply:
6 12
17 34
24 48
after transpose:
1 4 7
2 5 8
3 8 9

MyAnswer
import java.rmi.MarshalException;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int row = scan.nextInt();
        int col = scan.nextInt();
        System.out.println("row:" + row + " column:" + col);
        Matrix matrix = new Matrix(row,col);
        for(int i=0; i