1024 科学计数法
科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [+-][1-9].
[0-9]+E[+-][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。
现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。
输入格式:
每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。
输出格式:
对每个测试用例,在一行中按普通数字表示法输出 A,并保证所有有效位都被保留,包括末尾的 0。
输入样例 1:
+1.23400E-03
输出样例 1:
0.00123400
输入样例 2:
-1.2E+10
输出样例 2:
-12000000000
代码长度限制
16 KB
时间限制
200 ms
内存限制
64 MB
解答:
A_integer,A_decimals = input().split('E')
symbol_integer = A_integer[0]
symbol_decimals = A_decimals[0]
remove_step = int(A_decimals[1:]) #用eval不行
A_integer,A_decimals = list(A_integer),list(A_decimals) #因为下面用list方法
if symbol_decimals == '-':
A_integer.remove('.')
# A_integer.replace('.',''),字符串方法
for i in range(remove_step):
A_integer.insert(1,'0')
A_integer.insert(2,'.')
if symbol_decimals == '+':
# index_point = A_integer.index('.')
if (len(A_integer) - 3) > remove_step:
A_integer.remove(".")
A_integer.insert(2+remove_step,'.')
elif (len(A_integer) - 3) == remove_step:
A_integer.remove(".")
else:
A_integer.remove(".")
for i in range(remove_step - len(A_integer) + 2):
A_integer.append('0')
if symbol_integer == '+':
A_integer.pop(0)
# print(str(A_integer))
print(''.join(A_integer))
本来的思路是通过乘法运算来运算出数字,可是保留所有位数,小数点后0还要去处理,所以干脆使用字符串的方式。
1.
remove_step = int(A_decimals[1:])来去掉正负号,看移动位数。比如:-03。但一开始用eval不行,因为前面有0。
2.字符串通过list转换为列表进行插入删除等操作:
列表删除数据的方法:
「Python」列表删除数据的4个操作方法是哪些_神仙阿姨的博客-CSDN博客_python 列表删除
列表的插入:
Python中列表元素增加的三种方式_G啥也不会的博客-CSDN博客_python列表添加元素的三种方法
list.append(元素):把任意类型元素添加到末尾
list.insert(索引,新增的元素):在指定位置插入
list.extend(list2):把一个列表加到末尾
3.字符串与列表的转换
Python字符串与列表之间的转换方法_alicee_2012的博客-CSDN博客_python字符串转化为列表
字符串到列表:list(字符串),s.split()
列表转为字符串:
str和join
“str”对象没有属性“remove”,如何修复?
my_str = my_str.replace('b', '')
1048 数字加密
本题要求实现一种数字加密方法。首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余——这里用 J 代表 10、Q 代表 11、K 代表 12;对偶数位,用 B 的数字减去 A 的数字,若结果为负数,则再加 10。这里令个位为第 1 位。
输入格式:
输入在一行中依次给出 A 和 B,均为不超过 100 位的正整数,其间以空格分隔。
输出格式:
在一行中输出加密后的结果。
输入样例:
1234567 368782971
输出样例:
3695Q8118
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解答:
本来想
用这种方法计算,但是报错:字符和字符不能相减。
在C语言等高级语言中,字符之间的减运算都是支持的,但是python不然,在python中直接进行字符减运算是不被允许的。
A,B = input().split()
res = ['0','1','2','3','4','5','6','7','8','9','J','Q','K']
if len(A) < len(B):
temp = '0'*(len(B) - len(A))
A = temp + A
elif len(B) < len(A):
temp = '0'*(len(A) - len(B))
B = temp + B
flag = 1
C = ''
for i in range(len(A)-1,-1,-1):
if flag == 1:
temp = res[(int(A[i]) + int(B[i]))% 13]
C = temp + C
flag = 2
elif flag == 2:
temp = int(B[i]) - int(A[i])
if temp < 0:
temp += 10
temp = str(temp)
C = temp + C
flag = 1
print(C)
注意点:
1,如果是逆序,range(10,0,-1)。注意顺序
2,可以利用列表和下标来得出结果,而不用去判断结果是否大于9来转换
3,注意字符串可以相加,注意加的方向
4,注意不可以像c语言中一样,把两个字符进行加减
1001 A+B Format
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
第一次解:
又是插入元素后数组下标变化,但插入后面元素,前面元素下标不变,所以可以利用下标插入。繁琐的是要找规律。
一开始写错,以为是begin = len(temp) - 4,因为往前数了四个位置,在第四个位置插入。但是这道题是从后往前数。从前往后数时,插入后其他下标向后移,所以是4。但是这道题,比如我们要插到倒数2的位置,实际上往前数一位就好了。因为插入到倒数2的位置,它的位置就是原来倒数1的位置,原来的倒数1后移!
a,b = map(eval,input().split())
temp = list(str(a+b))
if temp[0] == '-':
flag = -1
temp.pop(0)
begin = len(temp) - 3
for i in range(begin,0,-3):
temp.insert(i, ',')
if flag == -1:
temp.insert(0,'-')
print("".join(temp))
修改后:
答案正确,原因是flag一开始没定义,正数会出错。
a,b = map(eval,input().split())
temp = list(str(a+b))
flag = 0
if temp[0] == '-':
flag = -1
temp.pop(0)
begin = len(temp) - 3
for i in range(begin,0,-3):
temp.insert(i, ',')
if flag == -1:
temp.insert(0,'-')
print("".join(temp))
第二次解:
开辟一个新数组,注意符号输出,注意第一位前不用加逗号
a,b = map(eval,input().split())
temp = list(str(a+b))
flag = 0
if temp[0] == '-':
flag = -1
temp.pop(0)
R = []
num = 1
for i in range(len(temp)-1,0,-1):
if num != 3:
R.insert(0,temp[i])
num += 1
else:
R.insert(0,temp[i])
R.insert(0,',')
num=1
R.insert(0,temp[0])
if flag == -1:
R.insert(0,'-')
print("".join(R))