刷题经验整理
python
- 输入输出遇到 EOFError
原因:input() 函数遇到了 EOF 不会自行处理 解决方法:
- 使用
sys.stdin
import sys
for line in sys.stdin:
a = int(line)
if a!= 0:
print(a)
- 使用
try...except EOFError...
try:
a = input()
except EOFError:
pass
- 大小写转换
s = input().lower()
s = ipnut().upper()
-
进制转换
- 别的进制转十进制
int('0xA', 16) #10 int('0o12', 8) #10 int('1101',2) #13- 别的进制转二进制
bin(0o12) #1010- 别的进制转八进制
oct(0xA) #0o12- 别的进制转十六进制
hex(0o12) #0xA -
数组元素类型转换 解决方法:使用推导式,根据原数组逐个进行类型转换,重新构建一个数组。
-
字典排序 解决方法:
sorted(dic.items()) -
字符串反序 解决方法:
- 用切片
a[::-1] - 构造新列表,循环遍历,完成反序;
- 转换成
list再使用reverse()函数 再使用join()函数;
- 用切片
-
计算字符的 ascii 码值 解决方法:使用
ord()关键字
java
常见输入输出总结
单一的数字输入
Scanner input = new Scanner();
int a = input.nextInt();
int b = input.nextInt();
循环输入单个的数字
Scnaner input = new Scanner();
while(input.hasNext()){
int a = input.nextInt();
int b = input.nextInt();
}
输入字符串
输入未知长度的数组
Scanner in = new Scanner(System.in);
String[] str = in.nextLine().split("");
int[] heights = new int[str.length];
for(int i=0; i<str.length; i++){
heights[i] = Integer.parseInt(str[i]);
}