标签归档:Python3

自测-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

编译器:Python(python3)

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

编译器:Python(python3)

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)

一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A​0​​A​1​​⋯AN−1​​)变换为(ANM​​⋯AN−1​​A​0​​A​1​​⋯ANM−1​​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

输入格式:

每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。

输出格式:

在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

输入样例:

6 2
1 2 3 4 5 6

输出样例:

5 6 1 2 3 4

编译器:Python(python3)

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)

让我们定义dn​​为:dn​​=pn+1​​−pn​​,其中pi​​是第i个素数。显然有d​1​​=1,且对于n>1有dn​​是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N(<10​5​​),请计算不超过N的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N

输出格式:

在一行中输出不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4

编译器:Python(python3)

 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

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
https://zh.wikipedia.org/wiki/%E5%9F%83%E6%8B%89%E6%89%98%E6%96%AF%E7%89%B9%E5%B0%BC%E7%AD%9B%E6%B3%95
中文的wiki上有现成的代码

自测-1 打印沙漏 (PTA)

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

*****
 ***
  *
 ***
*****

所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入格式:

输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。

输出格式:

首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。

输入样例:

19 *

输出样例:

*****
 ***
  *
 ***
*****
2

编译器:Python(python3)

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

编译器: Python(python3)

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[0],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)

给定K个整数组成的序列{ N​1​​, N​2​​, …, NK​​ },“连续子列”被定义为{ Ni​​, Ni+1​​, …, Nj​​ },其中 1≤ijK。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。

本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下:

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

输入格式:

输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。

输出格式:

在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。

输入样例:

6
-2 11 -4 13 -5 -2

输出样例:

20

编译器:Python(python3)

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)