当前位置: 代码迷 >> 综合 >> 用Java解决牛客网华为机试题目HJ91
  详细解决方案

用Java解决牛客网华为机试题目HJ91

热度:39   发布时间:2024-02-12 14:29:49.0

题目描述

请编写一个函数(允许增加子函数),计算n x m的棋盘格子(n为横向的格子数,m为竖向的格子数)沿着各自边缘线从左上角走到右下角,总共有多少种走法,要求不能走回头路,即:只能往右和往下走,不能往左和往上走。

输入描述:


 

输入两个正整数

输出描述:


 

返回结果

 

import java.io.IOException;
import java.util.*;public class Main {public static int gg(int n, int m) {int a[][] = new int[m + 1][n + 1];for (int i = 0; i < m + 1; i++) {for (int j = 0; j < n + 1; j++) {if (i == 0 && j == 0)a[i][j] = 1;else {if (i == 0 || j == 0)a[i][j] = 1;elsea[i][j] = a[i - 1][j] + a[i][j - 1];}}}return a[m][n];}public static void main(String[] args) throws IOException {Scanner cin = new Scanner (System.in);while (cin.hasNext()) {String line =cin.nextLine();int n = Integer.valueOf(line.substring(0, line.indexOf(" ")));int m = Integer.valueOf(line.substring(line.indexOf(" ") + 1));System.out.println(gg(n, m));}}}