http://poj.grids.cn/practice/3405/
Card Stacking
时间限制: 20000ms 内存限制: 65536kB
描述
Bessie is playing a card game with her N-1 (2 <= N <= 100) cow friends using a deck with K (N <= K <= 100,000; K is a multiple of N) cards. The deck contains M = K/N "good" cards and K-M "bad" cards. Bessie is the dealer and, naturally, wants to deal herself all of the "good" cards. She loves winning.
Her friends suspect that she will cheat, though, so they devise a dealing system in an attempt to prevent Bessie from cheating. They tell her to deal as follows:
1. Start by dealing the card on the top of the deck to the cow to her right
2. Every time she deals a card, she must place the next P (1 <= P <= 10) cards on the bottom of the deck; and
3. Continue dealing in this manner to each player sequentially in a counterclockwise manner
Bessie, desperate to win, asks you to help her figure out where she should put the "good" cards so that she gets all of them. Notationally, the top card is card #1, next card is #2, and so on.
输入
* Line 1: Three space-separated integers: N, K, and P
输出
* Lines 1..M: Positions from top in ascending order in which Bessie should place "good" cards, such that when dealt, Bessie will obtain all good cards.
样例输入
3 9 2
样例输出
3
7
8
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner cin = new Scanner(System.in); while(cin.hasNext()){ int n = cin.nextInt(); int k = cin.nextInt(); int p = cin.nextInt(); List<Integer> list = new ArrayList<Integer>(); List<Integer> list_o = new ArrayList<Integer>(); for(int i = 1;i < k + 1;++ i) list.add(i); int op = 1; int i = 0; while(!list.isEmpty()){ if(op % n == 0){ op = 1; list_o.add(list.get(i)); list.remove(i); }else{ list.remove(i); op ++; } i = i + p; if(list.size() > 0) i = i % list.size(); } Collections.sort(list_o); Iterator<Integer> it = list_o.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } }
在本文中,我们探讨了一款由Bessie及其牛友进行的卡牌游戏,通过制定策略来确保Bessie获得所有优质卡牌。文章详细介绍了游戏规则、策略实施步骤,并提供了代码示例,帮助理解如何通过特定操作获取优势。
803

被折叠的 条评论
为什么被折叠?



