赞
踩
计算a+b
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
数据范围: 数据组数 1≤t≤100 1 \le t \le 100 \ 1≤t≤100 , 数据大小满足 1≤n≤1000 1 \le n \le 1000 \ 1≤n≤1000
输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
输出a+b的结果
1 5
10 20
6
30
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int a = in.nextInt();
int b = in.nextInt();
if (a == 0 && b == 0) {
break;
}
System.out.println(a + b);
}
}
}
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { int sum = 0; int count = in.nextInt(); if (count == 0) { break; } for (int i = 0; i < count; i++) { sum += in.nextInt(); } System.out.println(sum); } } }
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String[] temp = in.nextLine().split(" ");
int sum = 0;
for (String str : temp) {
sum += Integer.parseInt(str);
}
System.out.println(sum);
}
}
}
输入有两行,第一行n
第二行是n个字符串,字符串之间用空格隔开
5
c d a bb e
a bb c d e
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 直接读掉就行
in.nextLine();
while (in.hasNextLine()) {
String[] temp = in.nextLine().split(" ");
Arrays.sort(temp);
System.out.println(String.join(" ", temp));
}
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
String s = Arrays.toString(arr);
System.out.println(s);
}
}
输出结果:
[1, 2, 3, 4, 5, 6]
比较项 | next( ) | nextLine( ) |
---|---|---|
说明 | 只能读取到空格之前的字符串 | 可以读取空格的字符串 |
比如“你好 java” | “你好” | “你好 java” |
使用前判断 | in.hasNext() | in.hasNextLine() |
String str = String.format()或者System.out.printf()
Math.ceil(1.01)
,向下取整:Math.floor(1.01)
标志 | 说明 | 示例 | 输出 |
---|---|---|---|
. | 后接保留多少位小数(四舍五入) | (“%.2f”,3.555) | 3.56 |
+ | 为正数或者负数添加符号 | (“%+d”,15) | +15 |
− | 左对齐 | (“%-5d”,15) | |15 | |
0 | 数字前面补0 | (“%04d”, 99) | 0099 |
核心要点:
2 * i + 1
,右孩子节点下标是2 * i + 2
。import java.util.*; public class Main { // 节点类 static class TreeNode { // 节点值 int val; // 左节点 TreeNode left; // 右节点 TreeNode right; // 节点的构造函数(默认左右节点都为null) public TreeNode(int x) { this.val = x; this.left = null; this.right = null; } // 根据数组构建二叉树 public TreeNode constructBinaryTree(final int[] arr) { // 构建和原数组相同的树节点列表 List<TreeNode> treeNodeList = arr.length > 0 ? new ArrayList<>(arr.length) : null; TreeNode root = null; // 把输入数值数组,先转化为二叉树节点列表 for (int i = 0; i < arr.length; i++) { TreeNode node = null; if (arr[i] != -1) { // 用-1表示null node = new TreeNode(arr[i]); } treeNodeList.add(node); if (i == 0) { root = node; } } // 遍历一遍,根据规则左右孩子赋值就可以了 // 注意这里 结束规则是 i * 2 + 1 < arr.length, 避免空指针 // 为什么结束规则不能是i * 2 + 2 < arr.length呢? // 如果i * 2 + 2 < arr.length是结束条件 // 那么i * 2 + 1这个符合条件的节点就被忽略掉了 // 例如[2, 7, 9, -1, 1, 9, 6, -1, -1, 10]这样的一个二叉树,最后的10就会被忽略掉 for (int i = 0; i * 2 + 1 < arr.length; i++) { TreeNode node = treeNodeList.get(i); if (node != null) { // 线性存储转链式存储关键逻辑 node.left = treeNodeList.get(2 * i + 1); // 再次判断,不忽略任何一个节点 if (i * 2 + 2 < arr.length) { node.right = treeNodeList.get(2 * i + 2); } } } return root; } } public static void main(String[] args) { } }
public class StringTest {
public static void main(String[] args) {
String str1 = "you";
String str2 = "welcome";
System.out.println(str1.concat(str2));
}
}
结果如下:
youwelcome
public class StringTest {
public static void main(String[] args) {
String str="we are students and he is a techer";
System.out.println(str.substring(2, 10));
}
}
结果如下:
are stu
public class StringTest {
public static void main(String[] args) {
String str = "我们一起数到6吧!";
System.out.println(str.indexOf("一"));
// Tests if this string starts with the specified prefix.
System.out.println(str.startsWith("我"));
// Tests if this string ends with the specified suffix.
System.out.println(str.endsWith("!"));
}
}
结果:
2
true
true
public class StringTest {
public static void main(String[] args) {
String s = "123.456 ";
double d = Double.parseDouble(s);
float f = Float.parseFloat(s);
System.out.println(d);
System.out.println(f);
}
}
String []s = str.split("\\.");
注意这里要对小数点转义,否则得不到正确答案。
StringBuffer支持以下几种操作函数:append()、insert()、replace()、delete()、reserve()等等。
public class StringTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3);
System.out.println(list);
}
}
public class StringTest {
public static void main(String[] args) {
Integer[] data = {1, 2, 3, 4};
Arrays.fill(data, 9);
System.out.println(Arrays.toString(data));
// [9, 9, 9, 9]
}
}
用指定元素填充数组,从起始位置到结束位置,取头不取尾 (会替换掉数组中原来的元素)
Integer[] data = {1, 2, 3, 4};
Arrays.fill(data, 0, 2, 9);
System.out.println(Arrays.toString(data)); // [9, 9, 3, 4]
public class StringTest {
public static void main(String[] args) {
String[] data = {"1", "2", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
Arrays.sort(data);
System.out.println(Arrays.toString(data)); // [1, 2, 3, 4]
}
}
public class StringTest {
public static void main(String[] args) {
String[] data = {"1", "2", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
// 实现降序排序,返回-1放左边,1放在右边,0保持不变
Arrays.sort(data, (str1, str2) -> {
if (str1.compareTo(str2) > 0) {
return -1;
} else {
return 1;
}
});
}
}
String[] data = {"1", "4", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
// 对下标[0, 3)的元素进行降序排序,即对1,4,3进行降序排序,2保持不变
Arrays.sort(data, 0, 3, (str1, str2) -> {
if (str1.compareTo(str2) > 0) {
return -1;
} else {
return 1;
}
});
System.out.println(Arrays.toString(data)); // [4, 3, 1, 2]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。