Java输出每个数字占5个空格,此输出模式见洛谷1443题
System.out.printf("%-5d",arr[i]);
Java输出每个数字四舍五入保留三位小数
System.out.printf("%.3f",num[i]);
C++输出每个数字占五个空格
printf("%-5d", a[i]);
C++输出每个数字四舍五入保留三位小数
printf("MEDIA = %.3lf",a[i]);
验证代码
public class Solution { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // 输出每个数字占五个空格 for (int i = 0 ; i < arr.length; i++) { System.out.printf("%-5d",arr[i]); } System.out.println(); double[] num = {1.0, 2.0, 3.0, 4.0, 5.0}; // 输出每个数字带有3个小数点 for (int i = 0; i < num.length; i++) { System.out.printf("%.3f",num[i]); } } }
笔试常用输入输出模板,后续继续完善
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { public static void main(String[] args) throws IOException { Read read = new Read(); // 接收单个数字 int n = read.nextInt(); int m = read.nextInt(); int[][] arr = new int[n][m]; // 接收n*m的数字类型的数组 for (int i = 0; i < n; i++) { for (int j = 0 ; j < m; j++) { arr[i][j] = read.nextInt(); } } // 接收一行字符串 String s = read.getStringLine(); // 输出一个数字后面加空格不换行 System.out.print(n + " "); // 输出一个字符串并换行 System.out.println(s); } } class Read { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer st = new StreamTokenizer(new InputStreamReader(System.in)); public int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } public double nextDouble() throws IOException { st.nextToken(); return st.nval; } public String nextString() throws IOException { st.nextToken(); return st.sval; } public String getStringLine() throws IOException { return reader.readLine(); } }