Skip to content

Instantly share code, notes, and snippets.

@shigero shigero/Cards.java
Last active Jan 4, 2016

Embed
What would you like to do?
15分ぐらいかなーって思ったら30分かかった/(^o^)\ http://ameblo.jp/programming/entry-10001721422.html
import java.util.Scanner;
class Cards {
public static void main(String[] args) {
System.out.print("numPlayers: ");
Scanner stdIn = new Scanner(System.in);
int numPlayers = stdIn.nextInt();
System.out.print("deck: ");
String deck = String.valueOf(stdIn.nextInt());
System.out.println("deal : ");
for (String s : deal(numPlayers, deck)) {
System.out.print("\"" + s + "\"" + ", ");
}
}
static String[] deal(int numPlayers, String deck) {
int cardsNumPerPlayer = deck.length() / numPlayers;
String [] dealResult = new String[numPlayers];
for (int i = 0; i < numPlayers; i++) { // i: 人のインデックス
dealResult[i] = "";
int j = i; // j: デッキのインデックス
while (dealResult[i].length() < cardsNumPerPlayer) {
dealResult[i] += deck.charAt(j);
j += numPlayers;
if (j > deck.length()) {
break;
}
}
}
return dealResult;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.