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};
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];
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)