当前位置: 首页 > news >正文

牛客OJ在线编程常见输入输出练习--Java版

目录

一、链接

二、题目


一、链接

牛客输入输出链接:牛客网 - 找工作神器|笔试题库|面试经验|实习招聘内推,求职就业一站解决_牛客网

二、题目

1.只有输出

public class Main {public static void main(String[] args) {System.out.println("Hello Nowcoder!");}
}

2.给定两个整数 a 和 b ,请你求出 a+b 的值。

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);}}
}

3.给定若干组测试数据,读取至文件末尾为止。
每组数据有两个整数 a 和 b ,请你求出 a+b 的值。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint a = in.nextInt();int b = in.nextInt();System.out.println(a + b);}}
}

4.给定 t 组测试数据。

每组数据有两个整数 a 和 b ,请你求出 a+b 的值。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint n=in.nextInt();for(int i=0;i<n;i++){int a = in.nextInt();int b = in.nextInt();System.out.println(a + b);}}}
}

5.给定若干组测试数据,最后一组数据为 0  0 ,作为输入的结尾。

每组数据有两个整数 a 和 b ,请你求出 a+b 的值。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint a = in.nextInt();int b = in.nextInt();// 跳出本次循环if(a==0 && b==0){continue;}System.out.println(a + b);}}
}

6.给定一个长度为 n 的正整数数组 a ,第 i 个元素的值为 ai 。
请你求出数组元素之和。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint n = in.nextInt();int[] arr = new int[n];long sum = 0;for(int i=0;i<n;i++){sum += in.nextInt();}System.out.println(sum);}}
}

7.给定 tt 组询问,每次询问给出一个长度为 nn 的正整数数组 aa ,第 ii 个元素的值为 aiai​ 。
请你分别求出每个数组的元素之和。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint t = in.nextInt();for(int i=0;i<t;i++){int n = in.nextInt();long sum = 0;for(int j=0;j<n;j++){sum += in.nextInt();}System.out.println(sum);}}}
}

8.给定一个 nn 行 mm 列的二维正整数数组 aa ,第 ii 行第 jj 列元素的值为 ai,jai,j​ 。
请你求出数组元素之和。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint n = in.nextInt();int m = in.nextInt();int[][] arr = new int[n][m];long sum = 0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){arr[i][j] = in.nextInt();sum+=arr[i][j];}}System.out.println(sum);}}
}

9.给定 tt 组询问,每次询问给出一个 nn 行 mm 列的二维正整数数组 aa ,第 ii 行第 jj 列元素的值为 ai,jai,j​ 。
请你分别求出每个数组的元素之和。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint t = in.nextInt();for (int k = 0; k < t; k++) {int n = in.nextInt();int m = in.nextInt();int[][] arr = new int[n][m];long sum = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {arr[i][j] = in.nextInt();sum += arr[i][j];}}System.out.println(sum);}}}
}

10.给定一个长度为 nn 的字符串 ss ,请你将其倒置,然后输出。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNext()) { // 注意 while 处理多个 caseint n = in.nextInt();String str = in.next();StringBuilder sb = new StringBuilder(str);str = sb.reverse().toString();System.out.println(str);}}
}

11.给定 tt 组询问,每次给出一个长度为 nn 的字符串 ss ,请你将其倒置,然后输出。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNext()) { // 注意 while 处理多个 caseint t = in.nextInt();for (int i = 0; i < t; i++) {int n = in.nextInt();String str = in.next();StringBuilder sb = new StringBuilder(str);str = sb.reverse().toString();System.out.println(str);}}}
}

12.给定一个 nn 行 mm 列的二维字符数组 aa ,第 ii 行第 jj 列元素的值为 ai,jai,j​ 。
请你对行和列都倒置,然后输出之。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNext()) { // 注意 while 处理多个 caseint n = in.nextInt();int m = in.nextInt();String[] strr = new String[n];for(int i=0;i<n;i++){String str = in.next();StringBuilder sb = new StringBuilder(str);str = sb.reverse().toString();strr[i] = str;}     for(int j=strr.length-1;j>=0;j--){System.out.println(strr[j]);}}}
}

13.给定 tt 组询问,每次给出一个长度为 nn 的带空格的字符串 ss ,请你去掉空格之后,将其倒置,然后输出。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNext()) { // 注意 while 处理多个 caseint t = in.nextInt();for(int i=0;i<t;i++){int n = in.nextInt();in.nextLine();String str = in.nextLine();//  将空格替换成无nullstr = str.replaceAll(" ", "");  System.out.println(new StringBuilder(str).reverse().toString());  }}}
}

14.给定一个小数 nn ,请你保留 33 位小数后输出。
如果原来的小数位数少于 33 ,需要补充 00 。
如果原来的小数位数多于 33 ,需要四舍五入到 33 位。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNext()) { // 注意 while 处理多个 casedouble i = in.nextDouble();System.out.println(String.format("%.3f",i));}}
}

15.给定一个正整数 nn ,请你保留 99 个数位,然后输出。
如果数位少于 99 个,那么需要补充前导零。

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint a = in.nextInt();int count = 0;int t = a;while(t>0){count++;t = t/10;}if(count==9){System.out.println(a);}if(count<9){int diff = 9-count;for(int i=0;i<diff;i++){System.out.print("0");}System.out.println(a);}}}
}

16.spj判断YES与NO

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint a = in.nextInt();if(a%2==0){System.out.println("no");}else{System.out.println("Yes");}}}
}

17.spj判断浮点误差

public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextInt()) { // 注意 while 处理多个 caseint a = in.nextInt();double s = 3.14 * a *a;System.out.print(s);}}
}

相关文章:

  • CE17.【C++ Cont】练习题组17(堆专题)
  • 18-总线IIC
  • Java大师成长计划之第25天:Spring生态与微服务架构之容错与断路器模式
  • 软件安全检测报告:如何全面评估企业级办公软件安全性?
  • .NET 中管理 Web API 文档的两种方式
  • Oracle APEX IR报表下载CSV文件的方法
  • lc42接雨水
  • 江协科技OLED移植hal库
  • gcc 源码目录文件夹功能简介
  • 2020CCPC河南省赛题解
  • c++动态链接库
  • 电子电路:电位器和可变电阻是同一个东西吗?
  • CT重建笔记(五)—2D平行束投影公式
  • [已解决] LaTeX “Unicode character“ 报错 (中文字符处理)
  • 硬件工程师笔记——二极管Multisim电路仿真实验汇总
  • 给图表组件上点“颜色” —— 我与 CodeBuddy 的合作记录
  • 赋能企业级移动应用 CFCA FIDO+提升安全与体验
  • 实物工厂零件画图案例(中)
  • 56.合并区间(java)
  • 【言语理解】逻辑填空之词义辨析(10)
  • 长沙最大的广告公司/seo顾问阿亮
  • 加强网站建设会/seo排名赚挂机
  • 郑州本地做团购的网站/百度权重划分等级
  • r语言做网站/怎么免费建个人网站
  • 百度公司做网站可靠吗/seo行业网
  • 网站建设毕业设计开题ppt/1688如何搜索关键词排名