close
題目概要:
Soha很忙,所以沒時間寫作業,幸好的朋友Sparrow願意幫他寫。寫一個程式,計算Sparrow是否願意幫忙Soha作業。
第一個整數為有多少個case(T)→整數代表此case Sparrow可以幫忙的科目數量→科目名稱以及做此功課所需要的時間(D)→每個case的最後即是Soha需要幫忙的科目以及繳交時間。
1繳交時間<=所需要的時間(D) : 輸出 Yesss
2. 繳交時間<=(所需要的時間+5)(D) : 輸出 Late
3. Soha需要幫忙的科目不在Sparrow的名單內 or 繳交時間>(所需要的時間+5)(D) : 輸出 Do your own homework!
解題方向:
1. 使用自定義的class存放科目名稱以及需多少天完成。
2. 使用Vector存放資料。(也可以用array替換)
3. 比對Soha需要幫忙的科目以及繳交時間再去作出對應的輸出。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
import java.util.Vector; | |
class uva11917{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
int T=sc.nextInt(); //多少筆資料。 | |
for(int i=0;i<T;i++){ | |
int N=sc.nextInt(); //Sparrow可以幫忙的科目數量。 | |
Vector<Data> vector=new Vector<Data>(); //使用Vectoe存放Sparrow可以幫忙的科目、需完成的天數。 Ps Data為自己定義。 | |
//存入Vector Sparrow可以幫忙科目、所需時間 | |
for(int j=0;j<N;j++){ | |
Data data=new Data(sc.next(),sc.nextInt()); | |
vector.add(data); | |
} | |
int D=sc.nextInt(); //要求科目所需的時間。 | |
String subject=sc.next(); //要求科目。 | |
//Output: Yesss or Late or Do your own homewok!。 | |
for(int j=0;j<vector.size();j++){ | |
if(vector.elementAt(j).getSt().equals(subject)){ | |
if(vector.elementAt(j).getD()<=D) System.out.println("Case "+(i+1)+": Yesss"); | |
else if(vector.elementAt(j).getD()<=(D+5)) System.out.println("Case "+(i+1)+": Late"); | |
else System.out.println("Case "+(i+1)+": Do your own homework!"); | |
break; | |
} | |
if(j==(vector.size()-1)) System.out.println("Case "+(i+1)+": Do your own homework!"); | |
} | |
} | |
} | |
} | |
//自行定義 存放科目名稱、所需時間。 | |
class Data{ | |
public Data(){} | |
public Data(String st,int D){ | |
this.st=st; | |
this.D=D; | |
} | |
public String getSt(){ | |
return st; | |
} | |
public int getD(){ | |
return D; | |
} | |
private String st; | |
private int D; | |
} |
文章標籤
全站熱搜