一行,输入两个整数n和m,用空格分隔,第二行包含n*m个整数(范围-231~231-1)。(1≤n≤10, 1≤m≤10)
输出规划后n行m列的矩阵,每个数的后面有一个空格。
2 3 1 2 3 4 5 6
1 2 3 4 5 6
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); int[] nums1 = new int[n * m]; int[][] nums2 = new int[n][m]; for (int i = 0; i < n * m; i++) { nums1[i] = in.nextInt(); } int k = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { nums2[i][j] = nums1[k++]; System.out.print(nums2[i][j] + " "); } System.out.println(); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ System.out.printf("%d ",sc.nextInt()); } System.out.println(); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); int n = scanner.nextInt(); int[][] Martix = new int[m][n]; while(scanner.hasNext()){ for(int i = 0;i < m;i++){ for(int j = 0;j < n;j++){ Martix[i][j] = scanner.nextInt(); System.out.print(Martix[i][j] + " "); } System.out.println(""); } } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sin = new Scanner(System.in); int n = sin.nextInt(); int m = sin.nextInt(); int[] nm = new int[n*m]; int i = 0; while(sin.hasNext()){ nm[i++] = sin.nextInt(); } // int[][] nms = new int[n][m]; int t = 0; for(int in =0;in<n;in++){ for(int im = 0;im<m;im++){ nms[in][im] = nm[t++]; } } // 调用方法 print(nms,n,m); } public static void print(int[][] nms,int n,int m){ int t = 0; for(int in =0;in<n;in++){ for(int im = 0;im<m;im++){ System.out.print(nms[in][im] + " "); } System.out.println(" "); } } }
import java.math.BigDecimal; import java.math.BigInteger; import java.util.HashSet; import java.util.Locale; import java.util.Random; import java.util.Scanner; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int m =sc.nextInt(); int n= sc.nextInt(); int s[][] = new int[m][n]; for(int i =0;i<m;i++){ for(int j= 0;j<n;j++){ s[i][j] = sc.nextInt(); } } for(int i[]:s){ for(int j :i){ System.out.print(j+" "); } System.out.println(); } } } }
import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int m = sc.nextInt(); List<Integer> list = new LinkedList<>(); for (int i = 0; i < n * m; i++) { list.add(sc.nextInt()); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.print(list.remove(0) + " "); } System.out.println(); } } } }
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(); int m=sc.nextInt(); for(int i=1;i<n*m+1;i++){ int temp; temp=sc.nextInt(); System.out.print(temp+" "); if(i%m==0){System.out.println();} } } }感谢各位大佬给的思路,c语言的太强了。我就写了个java的 想不到还有这样的操作