1.ZOJ 1078
Palindrom Numbers
Time Limit: 2000 msMemory Limit: 65536 KB
Statement of the Problem
We say that a number is a palindrom if it is the sane when read from left to right or from right to left. For example, the number 75457 is a palindrom.
Of course, the property depends on the basis in which is number is represented. The number 17 is not a palindrom in base 10, but its representation in base 2 (10001) is a palindrom.
The objective of this problem is to verify if a set of given numbers are palindroms in any basis from 2 to 16.
Input Format
Several integer numbers comprise the input. Each number 0 < n < 50000 is given in decimal basis in a separate line. The input ends with a zero.
Output Format
Your program must print the message Number i is palindrom in basis where I is the given number, followed by the basis where the representation of the number is a palindrom. If the number is not a palindrom in any basis between 2 and 16, your program must print the message Number i is not palindrom.
Sample Input
17
19
0
Sample Output
Number 17 is palindrom in basis 2 4 16
Number 19 is not a palindrom
题解:
def foo(num,basis):
s1='0123456789ABCDEF'
a=[]
new_string=''
while num>0:
y=num%basis
a.append(y)
num=num//basis
while a:
last=a.pop(-1)
new_string=new_string+s1[last]
return new_string
def check_palindrome(num):
str_num=str(num)
return str_num==str_num[::-1]
while True:
num=int(input())
good_base=[]
new_string=''
if num==0:
break
else:
for i in range(2,17):
new_num=foo(num,i)
if check_palindrome(str(new_num)):
good_base.append(i)
if not good_base:
print(f'Number {num} is not a palindrom')
else:
print(f'Number {num} is palindrom in basis', *good_base)
2.ZOJ 2812
Quicksum
Time Limit: 2000 msMemory Limit: 65536 KB
A checksum is an algorithm that scans a packet of data and returns a single number. The idea is that if the packet is changed, the checksum will also change, so checksums are often used for detecting transmission errors, validating document contents, and in many other situations where it is necessary to detect undesirable changes in data.
For this problem, you will implement a checksum algorithm called Quicksum. A Quicksum packet allows only uppercase letters and spaces. It always begins and ends with an uppercase letter. Otherwise, spaces and letters can occur in any combination, including consecutive spaces.
A Quicksum is the sum of the products of each character's position in the packet times the character's value. A space has a value of zero, while letters have a value equal to their position in the alphabet. So, A=1, B=2, etc., through Z=26. Here are example Quicksum calculations for the packets "ACM" and "MID CENTRAL":
ACM: 11 + 23 + 313 = 46 MID CENTRAL: 113 + 29 + 34 + 40 + 53 + 65 + 714 + 820 + 918 + 101 + 1112 = 650
Input: The input consists of one or more packets followed by a line containing only # that signals the end of the input. Each packet is on a line by itself, does not begin or end with a space, and contains from 1 to 255 characters.
Output: For each packet, output its Quicksum on a separate line in the output.
Example Input: | Example Output: |
ACM MID CENTRAL REGIONAL PROGRAMMING CONTEST ACN A C M ABC BBC # | 46 650 4690 49 75 14 15 |
题解:
try:
while True:
n = input()
sum = 0
if n == "#":
break
else:
b = []
for i in range(1, len(n) + 1):
for j in n:
if j == " ":
b.append(0)
else:
b.append(ord(j) - ord('A') + 1)
sum += i * b[i-1]
print(sum)
except EOFError:
pass
3.ZOJ 1383
Binary Numbers
Time Limit: 2000 msMemory Limit: 65536 KB
Given a positive integer n, print out the positions of all 1's in its binary representation. The position of the least significant bit is 0.
Example
The positions of 1's in the binary representation of 13 are 0, 2, 3.
Task
Write a program which for each data set:
reads a positive integer n,
computes the positions of 1's in the binary representation of n,
writes the result.
Input
The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.
Each data set consists of exactly one line containing exactly one integer n, 1 <= n <= 10^6.
Output
The output should consists of exactly d lines, one line for each data set.
Line i, 1 <= i <= d, should contain increasing sequence of integers separated by single spaces - the positions of 1's in the binary representation of the i-th input number.
Sample Input
1
13
Sample Output
0 2 3
t = int(input())
for i in range(t):
n = int(input())
b = bin(n)[2::]
s = 0
for j in range(len(b)):
if b[::-1][j] == '1':
if s == 1:
print(' ', end='')
print(j, end='')
s = 1
print('')