# 自测-5 Shuffling Machine (PTA)

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid “inside jobs” where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

```S1, S2, ..., S13,
H1, H2, ..., H13,
C1, C2, ..., C13,
D1, D2, ..., D13,
J1, J2
```

where “S” stands for “Spade”, “H” for “Heart”, “C” for “Club”, “D” for “Diamond”, and “J” for “Joker”. A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

### Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

### Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

### Sample Input:

```2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47
```

### Sample Output:

`S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5`

```spade = ['S' + str(i) for i in range(1, 14, 1)]
heart = ['H' + str(i) for i in range(1, 14, 1)]
club = ['C' + str(i) for i in range(1, 14, 1)]
diam = ['D' + str(i) for i in range(1, 14, 1)]
joker = ['J1', 'J2']
cards = spade + heart + club + diam + joker
result = [None]*len(cards)

K = int(input())
order = list(map(int, input().split()))

for i in range(len(cards)):
index = i
for j in range(K):
index = order[index] - 1
result[index] = cards[i]

print(' '.join(result)) ```

# 1023/自测-4 Have Fun with Numbers (PTA)

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

### Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

### Output Specification:

For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.

### Sample Input:

```1234567899
```

### Sample Output:

```Yes
2469135798```

```N = int(input())
doubleN = N*2

str_N = str(N)
str_doubleN = str(doubleN)

Set_N = set(str_N)
Set_doubleN = set(str_doubleN)

if Set_N == Set_doubleN:
print("Yes")
else:
print("No")

print(doubleN) ```

# 自测-3 数组元素循环右移问题 (PTA)

```6 2
1 2 3 4 5 6
```

### 输出样例:

`5 6 1 2 3 4`

```N, M = input().split(' ')
string = input().split(' ')
N = int(N)
M = int(M)
result = string[N-M:] + string[:N-M]
print(' '.join(result)) ```

# 自测-2 素数对猜想 (PTA)

```20
```

### 输出样例:

`4`

``` def eratosthenes(n):
IsPrime = [True] * (n + 1)
for i in range(2, int(n ** 0.5) + 1):
if IsPrime[i]:
for j in range(i * i, n + 1, i):
IsPrime[j] = False
return [x for x in range(2, n + 1) if IsPrime[x]]
n=int(input())
result = eratosthenes(n)
#print(result)
count = 0;
for i in range(len(result) - 1):
if result[i+1]-result[i]== 2:
count +=1;
print(count) ```

Python的问题还是消耗的资源多，用普通的算法最大N总是超时，这里使用了Sieve of Eratosthenes

# 自测-1 打印沙漏 (PTA)

```*****
***
*
***
*****
```

```19 *
```

### 输出样例:

```*****
***
*
***
*****
2```

```import numpy as np

num, ch =input().split(' ')
num = int(num)

N = np.sqrt((num+1)/2);

N = int(N)

residue = num - (2*np.square(N) - 1)

layers = 2*N - 1

for i in range(layers):
seq = abs(i - (N - 1)) +1
rep = 2*seq-1
str = rep*ch
zeros = (layers + rep)/2
zeros = int(zeros)
#str = str.center(layers,' ')
str = str.rjust(zeros)
print(str,end='\n')

print(residue) ```

# 01-复杂度2 Maximum Subsequence Sum (PTA)

Given a sequence of K integers { N​1​​, N​2​​, …, NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, …, Nj​​ } where 1≤ijK. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

### Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

### Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

### Sample Input:

```10
-10 1 2 3 4 -5 -23 3 7 -21
```

### Sample Output:

`10 1 4`

```def MaxSubseqSum4(A,N):
global ThisSum, MaxSum,start,end,temp
for i in range(N):
ThisSum += int(A[i])
if ThisSum > MaxSum:
MaxSum = ThisSum
start = temp
end = i
elif ThisSum < 0:
ThisSum = 0
temp = i+1
if MaxSum >= 0 :
print(MaxSum,A[start],A[end])
else:
print(0,A,A[N-1])

ThisSum = 0
MaxSum = -1
start = 0
end = 0
temp = 0

N = eval(input())
A = input().split(' ')
MaxSubseqSum4(A,N)   ```

# 01-复杂度1 最大子列和问题 (PTA)

• 数据1：与样例等价，测试基本正确性；
• 数据2：102个随机整数；
• 数据3：103个随机整数；
• 数据4：104个随机整数；
• 数据5：105个随机整数；

### 输入样例:

```6
-2 11 -4 13 -5 -2
```

### 输出样例:

`20`

```def MaxSubseqSum4(A,N):
global ThisSum, MaxSum
for i in range(N):
ThisSum += int(A[i])
if ThisSum > MaxSum:
MaxSum = ThisSum
elif ThisSum < 0:
ThisSum = 0
print(MaxSum)

ThisSum = 0
MaxSum = 0

N = eval(input())
A = input().split(' ')
MaxSubseqSum4(A,N)   ```