您有100张卡,编号为1到100。您将它们分配到k堆中,并按顺序回收。例如,如果将它们分为4堆,则第一堆将包含编号为1、5、9,…的卡,而第四堆将包含编号为4、8、12的卡…。首先是最后一堆,将其从下至上翻转,然后取下第三堆,将其从下至上翻转,然后将卡片放在第四堆的顶部,依此类推。在下一轮中,您将卡片分配到另一堆纸堆中,并以相同的方式收集(最后一堆和最后一堆)。
如果我们有10张纸牌并将它们分成2堆,则纸牌在纸堆中的顺序(从上到下)将是9、7、5、3、1和10、8、6、4、2
我们翻转桩以得到顺序1、3、5、7、9和2、4、6、8、10
我们将第二个桩放在底部,然后将第一个桩放在上面,以得到甲板1、3、5、7、9、2、4、6、8、10
给定轮数(m),每轮数(ki),您需要编写一个程序以在最后一轮结束时从顶部开始找到第N张卡片。
输入:一个数组arr [],表示每个回合中的堆数。
输出:玩完所有回合后代表第N张牌的一个整数。
限制条件:轮数≤10,每轮中桩数≤13。
例子:
Input: arr[] = {2, 2}, N = 4
Output: 13
We have two rounds. The first round has two piles.
At the end of the round, the deck is in the following order: 1, 3, 5, …, 99, 2, 4, 6, …, 100
The next round also has 2 piles and after the second round, the cards are in the order 1, 5, 9, 13, …
The fourth card from the top has number 13.
Input: arr[] = {2, 2, 3, 8}, N = 18
Output: 100
方法:对于每一轮,为每个堆创建空的ArrayList,然后按照问题中的说明在这些列表中插入数字(卡号),然后在每一轮之后更新原始卡号列表。在最后一轮结束时,从原始(更新的)列表中打印第n个整数。
下面是上述方法的实现:
Java
// Java implementation of the approach
import java.util.*;
class GFG {
// Total cards
static final int CARDS = 100;
// Function to perform the current round
static void currentRound(List list, int totalPiles)
{
// Create the required empty piles
List > piles = new ArrayList<>();
for (int i = 0; i < totalPiles; i++)
piles.add(new ArrayList());
// Add cards to the piles one by one
int j = 0;
for (int i = 0; i < CARDS; i++) {
piles.get(j).add(list.get(i));
j = (j + 1) % totalPiles;
}
// After all the piles have been reversed
// the new order will be first card of the
// first pile, second card of the first
// pile, ..., last pile of the last pile
// (from top to bottom)
int pileNo = 0, i = 0;
j = 0;
while (i < CARDS) {
list.set(i, piles.get(pileNo).get(j));
j++;
if (j >= piles.get(pileNo).size()) {
pileNo++;
j = 0;
}
i++;
}
}
// Function to perform all the rounds
static int performRounds(int piles[], int rounds, int n)
{
// Create the initial list with all the cards
List list = new ArrayList<>();
for (int i = 1; i <= CARDS; i++)
list.add(i);
// Perform all the rounds
for (int i = 0; i < rounds; i++)
currentRound(list, piles[i]);
// Return the nth card
return list.get(n);
}
// Driver code
public static void main(String[] args)
{
int piles[] = { 2, 2 };
int rounds = piles.length;
int n = 4;
// nth card will be at (n - 1)th index
n--;
System.out.print(performRounds(piles, rounds, n));
}
}
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Total cards
static int CARDS = 100;
// Function to perform the current round
static void currentRound(List list,
int totalPiles)
{
int i;
// Create the required empty piles
List > piles = new List>();
for (i = 0; i < totalPiles; i++)
piles.Add(new List());
// Add cards to the piles one by one
int j = 0;
for (i = 0; i < CARDS; i++)
{
piles[j].Add(list[i]);
j = (j + 1) % totalPiles;
}
// After all the piles have been reversed
// the new order will be first card of the
// first pile, second card of the first
// pile, ..., last pile of the last pile
// (from top to bottom)
int pileNo = 0; i = 0;
j = 0;
while (i < CARDS)
{
list.Insert(i, piles[pileNo][j]);
j++;
if (j >= piles[pileNo].Count)
{
pileNo++;
j = 0;
}
i++;
}
}
// Function to perform all the rounds
static int performRounds(int []piles,
int rounds, int n)
{
// Create the initial list with all the cards
List list = new List();
for (int i = 1; i <= CARDS; i++)
list.Add(i);
// Perform all the rounds
for (int i = 0; i < rounds; i++)
currentRound(list, piles[i]);
// Return the nth card
return list[n];
}
// Driver code
public static void Main(String[] args)
{
int []piles = { 2, 2 };
int rounds = piles.Length;
int n = 4;
// nth card will be at (n - 1)th index
n--;
Console.WriteLine(performRounds(piles, rounds, n));
}
}
// This code is contributed by PrinciRaj1992
13