■ カード配り問題に挑戦
■ ソースコード
public class Cards {
public String[] deal(int numPlayers, String deck){
String[] playerCards = new String[numPlayers];
int cardNum = deck.length();
//配列初期化
for(int i = 0; i < numPlayers; i++){
playerCards[i] = "";
}
//プレイヤーに同じ数のカードを配るために、余るカードは抜けておく
int amari = cardNum % numPlayers;
deck = deck.substring(0, cardNum - amari);
cardNum = deck.length();
//一人に一枚づつすら配れない場合は誰にもカードを配らない
if(cardNum == 0){ return playerCards; }
//カードを配る
for(int i = 0; i < cardNum; i++){
playerCards[i%numPlayers] += deck.charAt(i);
}
//結果を返す
return playerCards;
}
}
public class CardsTest {
public static void main(String[] args) {
int numPlayers;
String deck;
String[] results;
Cards cards = new Cards();
//例1
numPlayers = 6;
deck = "012345012345012345";
//Returns: {"000", "111", "222", "333", "444", "555" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例2
numPlayers = 4;
deck = "111122223333";
//Returns: {"123", "123", "123", "123" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例3
numPlayers = 1;
deck = "012345012345012345";
//Returns: {"012345012345012345" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例4
numPlayers = 6;
deck = "01234";
//Returns: {"", "", "", "", "", "" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例5
numPlayers = 2;
deck = "";
//Returns: {"", "" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
}
}
public String[] deal(int numPlayers, String deck){
String[] playerCards = new String[numPlayers];
int cardNum = deck.length();
//配列初期化
for(int i = 0; i < numPlayers; i++){
playerCards[i] = "";
}
//プレイヤーに同じ数のカードを配るために、余るカードは抜けておく
int amari = cardNum % numPlayers;
deck = deck.substring(0, cardNum - amari);
cardNum = deck.length();
//一人に一枚づつすら配れない場合は誰にもカードを配らない
if(cardNum == 0){ return playerCards; }
//カードを配る
for(int i = 0; i < cardNum; i++){
playerCards[i%numPlayers] += deck.charAt(i);
}
//結果を返す
return playerCards;
}
}
public class CardsTest {
public static void main(String[] args) {
int numPlayers;
String deck;
String[] results;
Cards cards = new Cards();
//例1
numPlayers = 6;
deck = "012345012345012345";
//Returns: {"000", "111", "222", "333", "444", "555" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例2
numPlayers = 4;
deck = "111122223333";
//Returns: {"123", "123", "123", "123" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例3
numPlayers = 1;
deck = "012345012345012345";
//Returns: {"012345012345012345" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例4
numPlayers = 6;
deck = "01234";
//Returns: {"", "", "", "", "", "" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
//例5
numPlayers = 2;
deck = "";
//Returns: {"", "" }
results = cards.deal(numPlayers, deck);
for(String result : results){
System.out.print("\""+result+"\" ");
}
System.out.println();
}
}
■ 出力結果
"000" "111" "222" "333" "444" "555"
"123" "123" "123" "123"
"012345012345012345"
"" "" "" "" "" ""
"" ""
ソースが無駄に長かった...。"123" "123" "123" "123"
"012345012345012345"
"" "" "" "" "" ""
"" ""
そして別にsubstringする必要が無かった...orz
//プレイヤーに同じ数のカードを配るために、余るカードは抜けておく
int amari = cardNum % numPlayers;
deck = deck.substring(0, cardNum - amari);
cardNum = deck.length();
上記は無駄。以下でokであることを確認...orzint amari = cardNum % numPlayers;
deck = deck.substring(0, cardNum - amari);
cardNum = deck.length();
cardNum -= cardNum % numPlayers;
いちいち文字列を作り直さないで、for文の繰り替えし数で調整すればいいじゃない。という話。うへぇ。
0 件のコメント:
コメントを投稿