杭电oj —— 2052

package com.demo2;

import java.util.Scanner;

/*
 * Give you the width and height of the rectangle(矩形),darw it.
 * 
 * Input contains a number of test cases.For each case ,
 * there are two numbers n and m (0 < n,m < 75)indicate the width and height of the rectangle.
 * Iuput ends of EOF.
 * 
 * For each case,you should draw a rectangle with the width and height giving in the input.
 * after each case, you should a blank line.
 * 
 */
public class HDU_oj2052 {
	public static void main(String[] args) {
		Scanner sn = new Scanner(System.in);
		while (sn.hasNext()) {
			int width = sn.nextInt();
			int height = sn.nextInt();
			width = width + 2;
			height = height + 2;
			/*
			 * width: 3 height:2 
			 * +---+ 
			 * |   | 
			 * |   | 
			 * +---+
			 */
			char[][] arr = new char[height][width];
			for (int i = 0; i < height; i++) {
				for (int j = 0; j < width; j++) {
					if (i == 0 || i == height - 1) {
						if (j == 0 || j == width - 1)
							arr[i][j] = '+';
						else
							arr[i][j] = '-';
					} else if ((i > 0 && i < height - 1) && (j == 0 || j == width - 1)) {
						arr[i][j] = '|';
					} else {
						arr[i][j] = ' ';
					}
				}
			}
			for (int i = 0; i < height; i++) {
				for (int j = 0; j < width; j++) {
					System.out.print(arr[i][j]);
				}
				System.out.println();
			}
			System.out.println();
		}
		sn.close();
	}
}

 

给定的引用中未包含杭电OJ第2049题的相关信息,所以无法直接从引用里获取该题的题目描述、解题思路和代码实现。不过通常杭电OJ第2049题是“不容易系列之(4)——考新郎”,下面为该题的相关信息: ### 题目描述 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做“考新郎”,具体的操作是这样的: 首先,给每位新娘打扮得几乎一模一样,并盖上大大的红盖头随机坐成一排; 然后,让各位新郎寻找自己的新娘.每人只准找一个,并且不允许多人找一个. 最后,揭开盖头,如果找错了对象就要当众跪搓衣板… 看来做新郎也不是容易的事情… 假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能. ### 解题思路 本题可分为两步来计算: 1. 从 `N` 个新郎中选出 `M` 个找错新娘的新郎,这是一个组合问题,组合数记为 $C_{N}^M$,计算公式为 $C_{N}^M=\frac{N!}{M!(N - M)!}$。 2. 计算 `M` 个新郎全部找错新娘的情况数,这是一个错排问题,错排数记为 $D(M)$。错排问题的递推公式为 $D(n)=(n - 1)(D(n - 1)+D(n - 2))$,其中 $D(1) = 0$,$D(2) = 1$。 最终结果就是 $C_{N}^M\times D(M)$。 ### 代码实现 ```python # 计算阶乘 def factorial(n): res = 1 for i in range(1, n + 1): res *= i return res # 计算组合数 C(n, m) def combination(n, m): return factorial(n) // (factorial(m) * factorial(n - m)) # 计算错排数 D(n) def derangement(n): if n == 1: return 0 if n == 2: return 1 dp = [0] * (n + 1) dp[1] = 0 dp[2] = 1 for i in range(3, n + 1): dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]) return dp[n] # 主函数 T = int(input()) for _ in range(T): N, M = map(int, input().split()) result = combination(N, M) * derangement(M) print(result) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值