Java acm输入
牛客练习网址:https://www.nowcoder.com/test/27976983/summary
hasNext hasNextLine
nextLine next nextInt
⽤Scanner实现字符串的输⼊有两种⽅法,⼀种是next(),⼀种nextLine()这两种⽅法的区别:
next()next⽅法不能得到带空格的字符串。
⽽nextLine()⽅法的结束符只是Enter键,即nextLine()⽅法返回的是Enter键之前的所有字符,它是可以得到带空格的字符串的
hasNext()和hasNextLine()方法的区别
采用hasNextXxxx()
的话,后面也要用nextXxxx()
:
- 比如前面用
hasNextLine()
,那么后面要用nextLine()
来处理输入; - 比如前面使用
hasNext()
方法去判断。后面用nextInt()
方法。
1、
输入描述:
输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
输出描述:
输出a+b的结果
示例1
输入
输出
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int a = in.nextInt(); int b = in.nextInt(); System.out.println(a + b); } } }
2、
输入描述:
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 1000)
输出描述:
输出a+b的结果
示例1
输入
2
1 5
10 20
输出
630
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n =sc.nextInt();
//判断n是否大于0,并-1操作 while(n-->0){ int b = sc.nextInt(); int a = sc.nextInt(); System.out.println(a+b); } } }
3、字符串
输入描述:
输入有两行,第一行n
第二行是n个字符串,字符串之间用空格隔开
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格
示例1
输入
5 c d a bb e
输出
a bb c d e
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); String[] a=new String[n]; for(int i=0;i<n;i++){ a[i]=sc.next(); } Arrays.sort(a); for(int i=0;i<n;i++){ System.out.print(a[i]+" "); } } }
3.2、字符串
输入描述:
输入有两行,第一行n
第二行是n个字符串,字符串之间用空格隔开
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格
输入例子1:
5 c d a bb e
输出例子1:
a bb c d e
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNextLine()) { int num = Integer.parseInt(in.nextLine()); String[] str = new String[num]; str = in.nextLine().split(" "); Arrays.sort(str); for(int i = 0; i < num; i++) { System.out.print(str[i] + " "); } } } }
4、hasNextLine()
输入描述:
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。
输出描述:
每组数据输出求和的结果
输入例子1:
1 2 3 4 5 0 0 0 0 0
输出例子1:
6 9 0
public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ String[] s =sc.nextLine().split(" "); int sum=0; for(int i=0;i<s.length;i++){ sum=sum+Integer.parseInt(s[i]); } System.out.println(sum); } }
5、不定长数组的输入
public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例 List<Integer> l = new ArrayList<>(); String[] str = null; //看这里!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! int res=0; str = in.nextLine().split(","); for(int i=0;i< str.length;i++){ int sum=0; for (int j=i;j< str.length;j++){ //if(Integer.valueOf(str[j])==Integer.valueOf(str[i])) continue; sum+= Integer.valueOf(str[j]); if (sum==0) res++; } } System.out.println(res); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律