目录
1.字符串逆序
Description
输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。
Input
输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。
Output
在一行中输出逆序后的字符串。
Sample Input 1
Hello World!
Sample Output 1
!dlroW olleH
代码:
s = input()
print(s[::-1])
2.四位数逆序输出
Description
编写一个程序,提示用户输入四位整数并以反向顺序显示。
Input
一个四位的整数。
Output
四个一位整数,在同一行输出,空格分隔,结尾无空格无换行。
Sample Input 1
1234
Sample Output 1
4 3 2 1
代码:
a = input()
for i in a[::-1]:
print(i,end=' ')
3.打印空心三角形
Description
输入任意一个大于零的整数N,根据输入的整数不同打印出不同行数的空心三角形。
Input
一个大于0的整数。
Output
一个空心三角形。
Sample Input 1
6
Sample Output 1
代码:
n = int(input())
for i in range(1,n):
for j in range(0,i):
if j==i-1:
print('*')
elif j==0:
print('*',end='')
else:
print(' ',end='')
for i in range(n):
print('*',end='')
4.删除字符串中连续的重复字符
Description
通过键盘输入一串小写字母(a~z)组成的字符串,编写一个测试程序,将字符串中连续出现的重复字符删去(即在一个字符串中,如果遇到连续重复的字符只出现一次),然后输出处理后的字符串。例如:str1=“aabbccddaabbccdd”,输出结果为:“abcdabcd”。
Input
共一行,为一个字符串。
Output
共一行,为删除连续的重复字符后得到的新字符串。
Sample Input 1
aabcccccaaa
Sample Output 1
abca
代码:
s = input()
ans = s[0]
for i in range(1,len(s)):
if s[i]!=s[i-1]:
ans+=s[i]
print(ans)
5.工作日的力量 - part one
Description
在一年365天中,常见的工作模式为“5+2”,即在一年365天,一周有5个工作日、2个休息日,假设在工作日阶段,每天进步1%;不工作时,每天退步1%?那么坚持一段时间下来取得的结果会是什么呢?为测试一下,请编写程序,实现计算通过n天后,今年能进步多少。要求:默认从本年的第一天开始努力,从键盘输入结束的天数。输出通过这段时间的努力获得力量之后的值。
Input
共一行,一个正整数,代表结束的天数(结束天数应介于0~365之间)。
Output
共一行,为一个浮点数,保留两位小数。
Sample Input 1
123
Sample Output 1
1.72
Sample Input 2
376
Sample Output 2
输入天数错误!
代码:
n = int(input())
if n >365 or n < 0:
print("输入天数错误!")
else:
ans = 1
for i in range(1,n+1):
if i%7 in [6,0]:
ans *= 0.99
else:
ans *= 1.01
print("%.2f" % ans)
6.你好,分段函数
Description
有这样一个函数,如果x大于0时,y等于x的三次方除以5,如果x等于0时,y的值为-1,如果x小于0时,y等于x的两倍加5。请输入相应的x的值,输出y的值。
无论结果是整数还是小数,请将结果保留1位小数输出。
Input
一个数,可以是整数,也可以是小数。
Output
一个数,保留一位小数输出。
Sample Input 1
0
Sample Output 1
-1.0
代码:
n = eval(input())
if n > 0:
print("%.1f" % (n ** 3 / 5))
elif not n:
print("-1.0")
else:
print("%.1f" % (n * 2 + 5))
7.字符串循环左移
Description
给定一个字符串S,要求把S的前k个字符移动到S的尾部,如把字符串“abcdef”前面的2个字符‘a’、‘b’移动到字符串的尾部,得到新字符串“cdefab”,称作字符串循环左移k位。
Input
输入在第1行中给出一个不超过100个字符长度的、以回车结束的非空字符串;第2行给出非负整数N。
Output
在一行中输出循环左移N次后的字符串。
Sample Input 1
abcd
2
Sample Output 1
cdab
代码:
s = input()
n = int(input())
for i in range(n,len(s)):
print(s[i], end = '')
for i in range(0,n):
print(s[i], end = '')
8.温度转换
Description
温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fahrenheit)。请编写程序将用户输入华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度。转换算法如下:(C表示摄氏度、F表示华氏度)C = ( F - 32 ) / 1.8;F = C * 1.8 + 32。
要求如下:
(1) 输入输出的摄氏度可采用大小写字母C结尾,温度可以是整数或小数,如:12.34C指摄氏度12.34度;
(2)输入输出的华氏度可采用大小写字母F结尾,温度可以是整数或小数,如:87.65F指华氏度87.65度;
(3)输出保留小数点后两位,输入格式错误时,输出提示:输入格式错误;
(4) 使用input()获得测试用例输入时,不要增加提示字符串。
Sample Input 1
12.34C
Sample Output 1
54.21F
Sample Input 2
12.34
Sample Output 2
输入格式错误!
Sample Input 3
12.34c
Sample Output 3
54.21f
代码:
t = input()
a, b, c, flag = 0, 0, t[-1], False
if c != 'F' and c != 'f' and c != 'C' and c != 'c':
print("输入格式错误!")
else:
for i in range(len(t)-1):
if t[i] == '.':
flag = True
elif not flag:
a = a * 10 + int(t[i])
else:
b = b * 10 + int(t[i])
n = a + b / 100
if c == 'F':
print("%.2fC" % ((n - 32) / 1.8))
elif c == 'f':
print("%.2fc" % ((n - 32) / 1.8))
elif c == 'C':
print("%.2fF" % (n * 1.8 + 32))
else:
print("%.2ff" % (n * 1.8 + 32))
9.字符串截取
Description
输入一个长度为10的字符串,截取其中第4-5和第7-8两个区域并显示。
Input
一个长度为10的字符串。
Output
输出两个切片区域,中间无间隔。
Sample Input 1
我喜欢去故宫和长城。
Sample Output 1
故宫长城
代码:
s = input()
print(s[4:6]+s[7:9])
10.判断回文字符串
Description
输入一个字符串,判断该字符串是否为回文。回文就是字符串中心对称,从左向右读和从右向左读的内容是一样的。
Input
输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。
Output
输出在第1行中输出字符串。如果它是回文字符串,在第2行中输出Yes,否则输出No。
Sample Input 1
level
Sample Output 1
level
Yes
代码:
s = input()
print(s)
if s == s[::-1]:
print("Yes")
else:
print("No")