acm输入练习
(1)
输入描述:
输入中每行是一对a和b。其中会有一对是0和0标志着输入结束,且这一对不要计算。
输入:
2 4
11 19
0 0
处理:
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){ // 检查下一行是否还有整数!!
int a = sc.nextInt();
int b = sc.nextInt();
if(a == 0 && b == 0) break;
System.out.println(a + b);
}
(2)
输入描述:
每行的第一个数N,表示本行后面有N个数。
如果N=0时,表示输入结束,且这一行不要计算。
输入:
4 1 2 3 4
5 1 2 3 4 5
0
处理:
Scanner sc = new Scanner(System.in);
Sting s = sc.nextLine(); // 读取一整行,并使得sc的指针跳转到下一行开始处!!
Sting t = sc.nextLine();
**(2)**
输入描述:
第一行输入一个长度为len,仅由小写字母组成的字符串s。
第二行输入一个长度为len,仅由小写字母组成的字符串t。
```plaintext
输入:
awaabb
aawbb
处理:
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
if (n==0) break;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += sc.nextInt();
}
sc.nextLine(); // 关键,这个函数使得跳转到下一行!
System.out.println(sum);
}
(3)
输入描述:
输入描述:
第一行输入两个整数 h,w(1≦h,w≦100) 代表迷宫的行数和列数。
此后 ℎ行,第 i 行输入 w 个整数 代表迷宫的布局
输入:
5 5
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
处理:
Scanner sc = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (sc.hasNextInt()) { // !!!
int n = sc.nextInt();
int m = sc.nextInt();
// 构造迷宫
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = sc.nextInt(); // !!!
}
}
(4)
输入描述:
在一行上:
先输入一个整数 n(1≦n≦10) 代表链表中节点的总数;
随后输入一个整数 h(1≦h≦10) 代表头节点的值;
随后输入 n−1 个二元组 (a,b)(1≦a,b≦10) ;
最后输入一个整数 k ,代表需要删除的节点值。
除此之外,保证每一个 b 值在输入前已经存在于链表中;每一个 a 值在输入前均不存在于链表中。节点的值各不相同。
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { // !!
int total = sc.nextInt();
int head = sc.nextInt();
List<Integer> linkedlist = new ArrayList<>();
linkedlist.add(head);
for (int i = 0; i < total - 1; i ++) {
int value = sc.nextInt(); // !!
int target = sc.nextInt(); // !!
linkedlist.add(linkedlist.indexOf(target) + 1, value);
}
int remove = sc.nextInt();
(5)
输入描述:
每行包含一个字符和一个整数n(0<n<41)
输入:
X 2
A 7
处理:
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { \\ 关键!
String s = sc.next(); \\ 只能用这种方法获取单个字符!!,获取一行字符则使用sc.nextLine()
int n = sc.nextInt();
}
TGZ
本文作者:torrentgz
本文链接:https://www.cnblogs.com/torrentgz/p/18746817
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步