字符串处理
大数运算处理
1 | 题目描述 |
1 | 输入描述: |
1 |
|
主要实现大数相加(进位)
大数相减(借位)
python解题代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86import sys
def BigNumAdd(Num1,Num2):
A=map(int,list(Num1))
B=map(int,list(Num2))
L1=len(A)
L2=len(B)
A.reverse()
B.reverse()
if L1>L2:
Max=A
Min=B
else:
Max=B
Min=A
jinwei=0
d=list()
for i in range(len(Min)):
jinwei=Min[i]+Max[i]+jinwei
baoliu=jinwei%10
jinwei=jinwei/10
d.insert(0,str(baoliu))
if i==len(Min)-1 and jinwei>0 and L1==L2:
d.insert(0,str(jinwei))
if L1!=L2:
for i in range(len(Min),len(Max)):
baoliu=jinwei+Max[i]
jinwei=baoliu/10
baoliu=baoliu%10
d.insert(0,str(baoliu))
if i==len(Max)-1 and jinwei>0:
d.insert(0,str(jinwei))
d=''.join(d)
return d
def BigNumSub(Num1,Num2):
Max=max(Num1,Num2)
Min=min(Num1,Num2)
if Max==Min:
return '0'
Max=map(int,list(Max))
Min=map(int,list(Min))
Max.reverse()
Min.reverse()
d=list()
jiewei=0
for i in range(len(Min)):
baoliu=Max[i]-Min[i]-jiewei
if baoliu<0:
baoliu=baoliu+10
jiewei=1
else:
jiewei=0
d.insert(0,str(baoliu))
d=''.join(d)
return d
def func(Num1,Num2):
if Num1[0]=='-' and Num2[0]=='-':
a=Num1[1:]
b=Num2[1:]
r='-'+BigNumAdd(a,b)
elif Num1[0]!='-' and Num2[0]!='-':
r=BigNumAdd(Num1,Num2)
else:
M=max(Num1,Num2)
T=min(Num1,Num2)
flag=0
if T[1:]>M:
flag=1
r=BigNumSub(T[1:],M)
if flag==1:
r='-'+r
return r
while True:
try:
num1=sys.stdin.readline().strip()
num2=sys.stdin.readline().strip()
r=func(num1,num2)
print r
except:
break
进制转化
1 | 题目描述 |
1 | 示例1 |
需要注意的是输入的x必须是数字类型,不能是字符了;类型
bin()、oct()、hex()的返回值均为字符串,且分别带有0b、0o、0x前缀。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24import sys
def numChange(Num):
T=list(Num[2:])
T.reverse()
sum=0;
for i in range(len(T)):
if T[i].isalpha():
t=ord(T[i])-ord('A')+10
else:
t=int(T[i])
sum+=t*(16**i)
return sum
while True:
try:
st=raw_input()
r=numChange(st)
print r
except:
break
关于牛客网中的sys.stdin.readline().strip()和raw_input()输入函数,优先选择raw_input,会把换行符去掉,通过概率更高!!!!
用内置函数int1
2
3
4
5while True:
try:
print int(raw_input(),16)
except:
break
字符串加密
1 | 题目描述 |
1 | 输入描述: |
代码实现的时候需要注意字母大小写的统一化,统一成大写就方便了
自己的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45import sys
def generateKey(S1):
S=S1.upper()
L=list()
for i in S:
if i in L:
continue
else:
L.append(i)
d={}
index=0
for i in L:
t=chr(ord('A')+index)
index+=1
d[t]=i
M=list()
for i in range(26):
t=chr(ord('A')+i)
if t in L:
continue
else:
M.append(t)
for i in M:
t=chr(ord('A')+index)
index+=1
d[t]=i
return d
def encode(S1,d):
S=S1.upper()
r=list()
for i in S:
t=d[i]
r.append(t.lower())
r=''.join(r)
return r
while True:
try:
k=raw_input()
c=raw_input()
d=generateKey(k)
r=encode(c,d)
print r
except:
break;
别人的代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16while True:
try:
lineCode , line = raw_input() , raw_input()
code = []
res = ''
for i in lineCode:
if i not in code:
code.append(i)
for i in range(97,123):
if chr(i) not in code:
code.append(chr(i))
for i in line:
res += code[ord(i)-97]
print res
except:
break
尼克切斯定理
1 | 题目描述 |
这道题的解法就是求出中位数,确定前后位置遍历一遍解就出来了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24def Fenjie(N):
t=int(N)
T=t**3
Ave=T/t
d=list()
if t%2==0:
St=Ave-t+1
Et=Ave+t-1
else:
St=Ave-t+1
Et=Ave+t-1
for i in range(St,Et+2)[::2]:
d.append(str(i))
r='+'.join(d)
return r
while True:
try:
n=int(raw_input())
r=Fenjie(n)
print r
except:
break
火车进站问题
1 | 题目描述 |
1 | 示例1 |
1 | 这道题问题的描述进一步解释是就是A[1..9]中转到C,C再转到B |
python的解法相当清晰
A–>C
C–>B1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34import copy
result=list()
def func(A,C,B):
if len(A)==0 and len(C)==0:
result.append(' '.join(B))
else:
if len(C)>0:
temp_A=copy.deepcopy(A)
temp_C=copy.deepcopy(C)
temp_B=copy.deepcopy(B)
temp_B.append(temp_C.pop())
func(temp_A,temp_C,temp_B)
if len(A)>0:
temp_A=copy.deepcopy(A)
temp_C=copy.deepcopy(C)
temp_B=copy.deepcopy(B)
temp_C.append(temp_A.pop(0))
func(temp_A,temp_C,temp_B)
while True:
try:
n=int(raw_input())
st=raw_input().split()
pre=st
In=[]
After=[]
func(pre,In,After)
result.sort()
for rs in result:
print rs
except:
break