单行输入
单行输入
每行输入三个值
Scanner input = new Scanner(System.in);
while(input.hasNextInt()){ //用input.hasNext()也行
int a = input.nextInt();
int b = input.nextInt();
int s = input.nextInt();
}
每次输入不固定长度的一行整数
public static void main(String[] args) {
Solution a = new Solution();
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String nextLine = sc.nextLine();
String[] split = nextLine.split(" ");
int [] b=Arrays.asList(split).stream().mapToInt(Integer::parseInt).toArray();
System.out.println(a.candy(b));
}
}
}
多行输入
例子1:
2
1 2 1
2 2 1
Scanner cin=new Scanner(System.in);
int T,c;
long n,m;
while (cin.hasNext()) {
T=cin.nextInt();
for (int i = 0; i < T; i++) {
n=cin.nextInt();
m=cin.nextInt();
c=cin.nextInt();
System.out.println((n/2+m/2)<=c?"Yes":"No");
}
}
例子2:
4 3
1 1 2 3
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(), m = scanner.nextInt();
int[] sds = new int[n];
for (int i = 0; i < sds.length; i++) {
sds[i] = scanner.nextInt();
}
牛客python输入输出
外循环加个While 1
每次输入一个数字
n = int(input())#输入只是一个数字
输入数字n和n行
'多行输入'
res = []
n = int(input())#行数
for _ in range(n):
s = input()
if s!='':
temp = [j for j in s.split()] #str输入
# temp = [int(j) for j in s.split()] #int输入
res.append(temp[0])
else:
break
print(res)
输出方式
for i in res:
print(F(i)) #每行只输出一个
# print(F(i), end=' ') #全在同一行内输出,用空格隔开
多行输入,行数未知
'多行输入,行数未知'
res = []
while True:
try:
s = input()
# res.append(list(map(int, s.split(' '))))
res.append(list(map(str, s.split(' '))))
except:
break
info = list(map(int,input().split(' ')))
a = []
b = []
for i in range(info[0]):
a.append(input().split(' '))
for j in range(info[1]):
b.append(input().split(' '))
print(a)
print(b)
https://blog.csdn.net/qq_39938666/article/details/101004633
赛码网有意思的题目
// 脑筋急转弯题hhh 走的路不小于起点到终点的纵横距离,并且多走的路是2的倍数。
// 乍看还以为是dfs的题呢~!/-\!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(input.hasNextInt()){
int a = input.nextInt();
int b = input.nextInt();
int s = input.nextInt();
int c = s - Math.abs(a) - Math.abs(b);
if(c<0 || c%2 != 0){
System.out.println("No");;
}else{
System.out.println("Yes");
}
}
}
}