将一个字符串中所有的整数前后加上符号“*”,其他字符保持不变。连续的数字视为一个整数。
数据范围:字符串长度满足
str1 = input() res = '' for i in range(len(str1)): if i == 0: if str1[i].isdigit() and str1[i+1].isdigit(): res = res + '*' + str1[i] elif str1[i].isdigit() and not str1[i+1].isdigit(): res = res + '*' + str1[i] + '*' else: res = res + str1[i] elif i == len(str1)-1: if str1[i].isdigit() and not str1[i-1].isdigit(): res = res + '*' + str1[i] + '*' elif str1[i].isdigit() and str1[i-1].isdigit(): res = res + str1[i] + '*' else: res = res + str1[i] else: if str1[i].isdigit() and not str1[i-1].isdigit() and str1[i+1].isdigit(): res = res + '*' + str1[i] elif str1[i].isdigit() and str1[i-1].isdigit() and not str1[i+1].isdigit(): res = res + str1[i] + '*' elif str1[i].isdigit() and not str1[i-1].isdigit() and not str1[i+1].isdigit(): res = res + '*'+str1[i] + '*' else: res = res + str1[i] print(res)
while True: try: n = input() n1 = ''#接收整个序列 n2 = '' #接收序列增长的那一个元素 for i in n: if i.isdigit():# i是数字 if not n2.isdigit():# i前一个不是数字,序列加*后再加i n1 += '*' else: if n2.isdigit():# i不是数字,i前一个是数字,序列加*后再加i n1 += '*' n1 += i n2 = i #每次接收序列增长的那一个元素,直到遍历完,接收最后一个元素 if n2.isdigit():#判断最后一个元素是否是数字,是的话啊需要最后加上* n1 += '*' print(n1) except: break
# 利用双指针 data = input() left,right = 0,0 ans ='' while left<len(data): if not data[left].isdigit(): ans+=data[left] left+=1 continue else: right = left while right<len(data) and data[right].isdigit(): right+=1 ans = ans+'*'+data[left:right]+'*' left=right print(ans)
int_number = [str(i) for i in range(10)] input_str = list(input()) for i in range(len(input_str)): if input_str[i] in int_number: if i == 0: input_str[i] = "*" + input_str[i] else: if input_str[i-1][-1] not in int_number: input_str[i] = "*" + input_str[i] if i == len(input_str)-1: input_str[i] = input_str[i] + "*" else: if input_str[i+1] not in int_number: input_str[i] = input_str[i] + "*" for c in input_str: print(c, end="") # for c in input_str: # if c not in int_number:
strr = input() output = '' if strr[0].isdigit(): output += '*' + strr[0] if strr[1].isdigit() == False: output += '*' else: output += strr[0] for i in range(1,len(strr)-1): if strr[i].isdigit() and strr[i-1].isdigit()==False: output = output + '*' + strr[i] else: output = output + strr[i] if strr[i].isdigit() and strr[i+1].isdigit() == False: output += '*' if len(strr)>=2: if strr[-1].isdigit(): if strr[-2].isdigit(): output += strr[-1] +'*' else: output += '*'+ strr[-1] +'*' else: output += strr[-1] print(output)
s = input() res = "" for i in range(len(s)): # 如果不是数字 if not s[i].isdigit(): # 如果res为空,或res最后一个字符不是数字 if not res&nbs***bsp;not res[-1].isdigit(): res += s[i] else: res += '*'+s[i] # 如果是数字 else: # 如果res为空,或者 res最后一个字符不是数字 if not res&nbs***bsp;not res[-1].isdigit(): res+= '*'+s[i] else: res += s[i] # 如果最后一位是数字的话 if s[-1].isdigit(): res+='*' print(res)
a = input() if a[0].isdigit():#如果第一位是数字 print('*',end='') for i in range(0,len(a)-1): print(a[i],end='') if a[i].isdigit() == False and a[i+1].isdigit():#当前不是数字,但下一位是数字 print('*',end='') if a[i].isdigit() and a[i+1].isdigit() == False:#当前是数字,但下一位不是数字 print('*',end='') print(a[-1],end='') if a[-1].isdigit(): #如果最后一位是数字 print('*',end='')
# 笨办法 s = input() n = list(map(str,list(range(10)))) ls = [] if s[0] in n: ls.append('*') for i in range(len(s)-1): ls.append(s[i]) if ((s[i] not in n) and (s[i+1] in n))&nbs***bsp;((s[i] in n) and (s[i+1] not in n)): ls.append('*') if s[-1] not in n: ls.append(s[-1]) else: ls.append(s[-1]) ls.append('*') print(''.join(ls))
a=input() b=list(a) i=0 j=-1 while i<=len(b): try: if i==len(b) and b[j].isdigit(): b.append('*') i+=1 j+=1 elif b[i].isdigit() and j==-1: b.insert(i,'*') i+=2 j+=2 elif j==-1 and not b[i].isdigit(): i+=1 j+=1 elif b[i].isdigit() and not b[j].isdigit(): b.insert(i,'*') i+=2 j+=2 elif not b[i].isdigit() and b[j].isdigit(): b.insert(i,'*') i+=2 j+=2 else: i+=1 j+=1 except: break for i in b: print(i,end='')超级笨办法
s = input() l = r = 0 ans = '' while r < len(s): if not s[r].isdigit():#如果不是数字 #开始结算上一个连续数字区间 num_len = r-l #没有连续数字时这个变量为0 if num_len != 0: ans += '*' + s[l:r] +'*' l = r #左指针复位 ans += s[l] r += 1 l += 1 else: r += 1 if l != r: ans += '*' + s[l:r] +'*' print(ans)
a = input() a = "xx" + a + "xx" b = [] j = 0 for i in range(len(a) - 1): t0, t1 = a[i].isdigit(), a[i + 1].isdigit() if t0.__xor__(t1): b.append(a[j : i + 1]) j = i + 1 b.append(a[j:]) print("*".join(b)[2:-2])