您有 100 张卡片,编号为 1 到 100。您将它们分成 k 堆并按顺序收集这些堆。例如,如果您将它们分成 4 堆,那么第一堆将包含编号为 1、5、9、……的卡,而第 4 堆将包含编号为 4、8、12、……的卡,同时收集您收集的卡首先是最后一堆,从下到上翻转,然后取第三堆,从下到上翻转,然后将卡片放在第 4 堆的顶部,依此类推。下一轮,您将卡片分配到另一组堆中并以相同的方式收集(最后一堆在前,第一堆在后)。
如果我们有 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
方法:对于每一轮,为每一堆创建空的 ArrayLists,然后按照问题中的描述将数字(卡号)插入这些列表中,然后在每轮之后更新原始卡号列表。在最后一轮结束时,打印原始(更新)列表中的第 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
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。