一行,输入两个整数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
#include<stdio.h> int main(void) { int n = 0; //row int m = 0; //col scanf("%d%d", &n, &m); int kiki[n][m]; //enter datas for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &kiki[i][j]); } } //output datas for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { static int line = 0; printf("%d ", kiki[i][j]); if (++line % m == 0) //m data per line printf("\n"); } //end for j } //end for i return 0; }
#include <stdio.h> #include <stdlib.h> int main(void) { int i, j, n, m; scanf("%d %d", &n, &m); int *a = (int *)malloc(sizeof(int) * n * m); if (NULL == a) { fprintf(stderr, "Memory allocation failed!\n"); exit(EXIT_FAILURE); } for (i = 0; i < n * m; i++) { scanf("%d", &a[i]); } for (i = 0; i < n * m; i++) { printf("%d ", a[i]); if (0 == (i + 1) % m) { putchar('\n'); } } return 0; }//避免嵌套循环的话时间复杂度就不会太高;
#include <stdio.h> int main() { int n = 0, m = 0; scanf("%d%d", &m, &n); int a = m * n; int arr[100] = { 0 }; for(int i = 0; i < a; i++) { scanf("%d", &arr[i]); } int k = 0; for(int i = 0; i < m; i++) { for(int j = 0 + k; j < n + k; j++) { printf("%d ",arr[j]); } k += n ; printf("\n"); } }
#include <stdio.h> int main() { int n, m = 0; int arr[10][10] = {0}; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &arr[i][j]); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }
#include <stdio.h> int main() { int m,n,a[11][11]; scanf("%d %d",&m,&n); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ scanf("%d",&a[i][j]); printf("%d ",a[i][j]); } printf("\n"); } return 0; }
#include<stdio.h> int main() { int n, m, arr[10][10]; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &arr[i][j]); printf("%d ", arr[i][j]); } printf("\n"); } return 0; }
#include <stdio.h> int main(){ int n, m, arr[10][10]; int i, j = 0; scanf("%d %d", &n, &m); for(i = 0; i < n * m; i++, j++) scanf("%d", &arr[i/m][j%m]); for(i = 0; i < n * m; i++, j++){ printf("%d ", arr[i/m][j%m]); if((j+1) % m==0) printf("\n"); } return 0; }
#include <stdio.h> int main() { int n,m; scanf("%d %d",&n,&m); int i,j,array[n][m]; int arr[n*m]; for(i=0;i<n*m;i++) { scanf("%d",&arr[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { array[i][j]=arr[i*m+j]; } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { printf("%d ",array[i][j]); } printf("\n"); } return 0; }
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(); } } }
#include <stdio.h> int main() { int a , b; scanf("%d %d",&a, &b); int arr1[1000]; int arr2[10][10]; int count=0; while (count<a*b) { for (int i=0; i<a*b; i++) { scanf("%d ",&arr1[i]); } for (int i=0; i<a; i++) { for (int j=0; j<b; j++) { arr2[i][j]=arr1[count]; printf("%d ",arr2[i][j]); count++; } printf("\n"); } } return 0; }
#include <stdio.h> #include <malloc.h> int main() { int n = 0; int m = 0; int i = 0; int j = 0; int* p = NULL; scanf("%d%d", &n, &m); //开辟空间 p = (int*)malloc(sizeof(int) * n * m); if(!p) { perror("malloc"); return 1; } //录入数据 for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { scanf("%d", p + (i * m) + j); } } //输出结果 for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { printf("%d ", *(p + (i * m) + j)); } printf("\n"); } //开辟空间 free(p); p = NULL; return 0; }
#include <stdio.h> int main() { int n, m; scanf("%d %d", &n, &m); int mn[10][10]; // 定义一个n行m列的矩阵 // 输入n*m个整数 for (int z = 0; z < n * m; z++) { scanf("%d", &mn[z / m][z % m]); } // 输出n行m列的矩阵 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { printf("%d ", mn[i][j]); } printf("\n"); // 输出每一行后换行 } return 0; }
int main() {
int n = 0; int m =0; int i =0; int num = 0; scanf("%d %d",&n,&m); for(i=1;i<n*m+1;i++) { scanf("%d ",&num); printf("%d ",num); if(i%m ==0) { printf("\n"); } } return 0;
}