KiKi得到了一个n行m列的矩阵,现在他想知道第x行第y列的值是多少,请你帮助他完成这个任务。
数据范围: ,矩阵中的值满足
第一行包含两个数n和m,表示这个矩阵包含n行m列。从2到n+1行,每行输入m个整数(范围-231~231-1),用空格分隔,共输入n*m个数,表示矩阵中的元素。接下来一行输入x和y,用空格分隔,表示KiKi想得到的元素的位置。(1≤x≤n≤10,1≤y≤m≤10)
一行,输出一个整数值,为KiKi想知道的值。
2 3 1 2 3 4 5 6 1 2
2
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(); int[][] num = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { num[i][j] = sc.nextInt(); } } int x = sc.nextInt(); int y = sc.nextInt(); System.out.println(num[x - 1][y - 1]); } } }
int main(){ int n, m, i, x, y; scanf("%d %d", &n, &m); int arr[5][5] = {0}; for(i = 0; i < n; i++){ for(int j = 0; j < m; j++){ scanf("%d ", &arr[i][j]); } } scanf("%d %d", &x, &y); printf("%d", arr[x-1][y-1]); return 0; }
#include<iostream> using namespace std; int main() { int n,m; cin>>n>>m; int** a=new int*[n]; for(int i=0;i<n;i++) a[i]=new int[m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cin>>a[i][j]; } int p,q; cin>>p>>q; cout<<a[p-1][q-1]<<endl; for(int i=0;i<n;i++) delete []a[i]; delete []a; return 0; }
using System; using System.Collections.Generic; using static System.Console; using System.Linq; using System.Text; class P { public static void Main() { var str=ReadLine().Split(); var (x, y) = (int.Parse(str[0]), int.Parse(str[1])); var array = new int[x, y]; for(int i=0;i<x;++i) { var tt=ReadLine().Split(); for (int j = 0; j < y; j++) { array[i, j] = int.Parse(tt[j]); } } var str_ = ReadLine().Split(); WriteLine(array[int.Parse(str_[0])-1, int.Parse(str_[1])-1]); } }
#include <stdio.h> #include <stdlib.h> int main() { int m,n; scanf("%d %d",&m,&n); int **p=(int**)malloc(m*sizeof(int*)); for(int i=0;i<m;i++){ p[i]=(int*)malloc(n*sizeof(int)); for(int j=0;j<n;j++)scanf("%d",&p[i][j]); } int x,y; scanf("%d %d",&x,&y); printf("%d",p[x-1][y-1]); return 0; }
using System; using System.Collections.Generic; using static System.Console; using System.Linq; using System.Text; public class Program { public static void Main() { string s = ReadLine(); string[] s1 = s.Split(' '); int n = Convert.ToInt32(s1[0]); int m = Convert.ToInt32(s1[1]); int[,] a = new int[n, m]; for (int i = 0; i < n; i++) { string[] s2 = ReadLine().Split(' '); for (int j = 0; j < m; j++) { a[i, j] = Convert.ToInt32(s2[j]); } } string[] s3 = ReadLine().Split(' '); int x = Convert.ToInt32(s3[0]); int y = Convert.ToInt32(s3[1]); int result = a[x - 1, y - 1]; WriteLine(result); } }
''' 20240225牛客练习 二维列表使用 BC129矩阵元素定位 1.情况1:有存在多组输入,目前只考虑了输入是固定三个行的如果多行无法解决 考虑多个循环进行重复输入,感谢评论区思路;对于输入要找的行列数肯定不会超限,只记录这些,同时可以节省一部分资源 ''' def input_list_num(): line,column = map(int ,input().split()) # print("输入的行是:",line,"输入的列是",column) list_all = [] for i in range(line): temp = list(input().split()) list_all.append(temp) # 想要获取的所在行和列 get_x ,get_y = map(int, input().split()) # print() print(list_all[get_x-1][get_y-1]) if __name__ == "__main__": input_list_num()
#include <stdio.h> int main() { int n,m = 0; int a,b = 0; int arr[100][100]; while(scanf("%d %d",&n,&m)!=EOF) { //获取输入的值 for(int i = 0;i<n;i++) { for(int j = 0;j<m;j++) { scanf("%d",&arr[i][j]); } } //输入要找值的位置 scanf("%d %d",&a,&b); //输出减1是因为从0开始 printf("%d",arr[a-1][b-1]); } return 0; }