2020年09月 HNUCM-OJ算法分析与设计作业9

这篇博客包含了HNUCM-OJ的算法练习,涉及击鼓传花问题的鼓点计算、字符串解密方法及数字三角形的最佳路径动态规划解法。通过实例和参考答案,帮助读者理解和解决这些问题。
摘要由CSDN通过智能技术生成

@ZHANGQIANYI2020
@[TOC](HNUCM-OJ 击鼓传花,Welcome,解密,数字三角形之备忘录法,数字三角形之动态规划法,最长公共子序列问题(LCS)之备忘录法)

问题 A: 击鼓传花

(时间限制: 1 Sec 内存限制: 128 MB)

题目描述:

岳麓山下旌旗卷,新月湖畔起歌声。湖南中医药大学运动会隆重开幕了,小 Q 所在的班级参加了“击鼓
传花”趣味比赛。
比赛规则如下,n 个队员 (不包括小 Q) 围成一个圈,每个人手里拿着一些花束 (数量也可能为 0),小 Q
站在圆圈的中间,队员需要传递花束给小 Q,每个鼓点只允许一个人传送一束花,最终要求圆圈上所有
人的花束都传递给小 Q。请问需要花多少鼓点呢?

输入:

多组数据。
第一行 n,代表队员数量。
第 2 行到第 n+1 行,每行一个整数 a[i],代表每个队员最开始拿的花束数量。
0 ≤ a[i] ≤ 106
所有组的 n 之和不超过 103

输出:

一个整数,代表需要的鼓点数。

样例输入:

4
1
2
5
4

样例输出:

12

参考答案:

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();
            long s = 0;
            for (int i = 1; i <= n; i++){
   
                int a=sc.nextInt();
                s+=a;
            }
            System.out.println(s);
        }
    }
}

问题 B: Welcome

(时间限制: 1 Sec 内存限制: 128 MB)
题目描述:

”How happy we are, To meet friends from afar!”
Welcome to Hunan University of Chinese Medicine!
Hope all of you can enjoy the competition ^ v ^
Now your task is to read an integer w and output the character painting of ”HNUCM”, there are w
space(s) (space are represented by dot) between two letters. Please refer to the sample for the specific
format.

输入:

There are several test files and each contains one case.
The input contains only 1 integer w (1 ≤ w ≤ 2018).

输出:

The output has 5 lines, each line has 25+4w characters which only contains ’o’(lowercase letter ’o’) and
’.’(English period ’.’)

样例输入:

1

样例输出:

o...o.o...o.o...o.ooooo.o...o
o...o.oo..o.o...o.o.....oo.oo
ooooo.o.o.o.o...o.o.....o.o.o
o...o.o..oo.o...o.o.....o...o
o...o.o...o.ooooo.ooooo.o...o

参考答案:

import java.util.Scanner;

public class Main {
    
	static int w = 0;
	public static void print1(){
   
		for (int i = 1; i <= w; i++)
			System.out.print(".");
	}
	private static void print2(){
   
		System.out.print("o...o");print1();System.out.print("o...o");print1();System.out.print("o...o");print1();System.out.print("ooooo");print1();System.out
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值