#RANK_2 1651:Multiplication Puzzle

总时间限制: 
1000ms
内存限制: 
65536kB
描述
The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000

If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.
输入
The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.
输出
Output must contain a single integer - the minimal score.
样例输入
6
10 1 50 50 20 5
样例输出
3650
#include <iostream>
#include <cstring>
using namespace std;
int N = 0;
int v[0x100];
int result[0x100][0x100];

int getscore ( int start, int end ){ //返回start到end区间的最小值 
    if (start + 2 > end ) return 0;
    if (result[start][end] != -1 ) return result[start][end];
    else {
        for ( int i = start + 1; i< end; i++){
            int tmp = v[i] * v[start] * v[end] + getscore( start , i) + getscore ( i, end );
            if ( tmp < result[start][end] || result[start][end] == -1 ){
                result[start][end] = tmp;
            }
        }
        return result[start][end];
    }
}
int main(){
    memset(result,-1,sizeof(result));
    cin >> N;
    for (int i = 1; i <= N ;i++){
        cin >> v[i];
    }
    cout << getscore( 1, N );
} 

解题思路:

运用递归的思路来解这道题

先考虑边界情况,即只剩三个数的时候,结果是固定的,即v[1]*v[i]*v[N],N为数据的个数。

我们假设f(a,b)可以返回区间内所有取法的minscore,便得到了递推式:

f(i,N)= min(  f(1,i)+f(i,N)+i*f(1)*f(N)  ); i取区间(1,N)的任意整数;

当a+2>b时,f(a,b) = 0 (另一个边界条件)。

 

同时使用记忆化搜索的策略,如果result存在直接调用,否则计算以后再行调用。

转载于:https://www.cnblogs.com/qianwagui/p/9064009.html

<think>我们正在讨论如何使用pytest.main()来运行测试。根据提供的引用,我们可以总结出以下几点关键信息: 1. **基本用法**:导入pytest模块并调用`pytest.main()`函数来执行测试(引用[^1])。 2. **参数传递**:`pytest.main()`可以接受参数,例如指定运行哪些测试用例(引用[^2])。 3. **参数生效问题**:有时传入的参数可能不生效,需要注意运行环境配置(引用[^3])。 下面我将逐步说明如何使用`pytest.main()`。 ### 步骤1: 导入pytest 首先,在Python脚本中导入pytest模块。 ### 步骤2: 编写测试用例 编写测试函数,函数名应以`test_`开头,这样pytest才能自动识别。 ### 步骤3: 调用pytest.main() 在脚本的适当位置(通常是`if __name__ == '__main__':`块中)调用`pytest.main()`。 #### 基本示例 ```python import pytest # 编写测试用例 def test_example(): assert 1 + 2 == 3 if __name__ == "__main__": pytest.main() ``` ### 步骤4: 传递参数 `pytest.main()`可以接受一个参数列表,用于指定运行行为。例如: - 运行指定目录:`pytest.main(['tests/'])` - 运行指定文件:`pytest.main(['test_module.py'])` - 运行指定测试类:`pytest.main(['test_module.py::TestClass'])` - 运行指定测试方法:`pytest.main(['test_module.py::TestClass::test_method'])` - 使用匹配关键词:`pytest.main(['-k', 'keyword'])` #### 带参数的示例 ```python if __name__ == "__main__": # 运行当前目录下所有测试 pytest.main() # 运行指定文件 pytest.main(['test_sample.py']) # 运行包含"example"关键词的测试 pytest.main(['-k', 'example']) # 显示详细输出 pytest.main(['-v']) ``` ### 注意事项 1. **参数不生效问题**:如果传入参数没有生效,可能是因为运行配置问题。确保没有其他运行配置(如IDE配置)覆盖了`pytest.main()`的参数。此外,检查是否在正确的环境中运行(引用[^3])。 2. **在脚本中调用**:当直接运行脚本时,使用`if __name__ == '__main__':`来调用`pytest.main()`,这样只有在脚本作为主程序运行时才会执行测试。 ### 示例:完整脚本 ```python import pytest def test_addition(): assert 1 + 1 == 2 def test_subtraction(): assert 3 - 1 == 2 class TestMath: def test_multiplication(self): assert 2 * 3 == 6 if __name__ == "__main__": # 可以在这里修改参数,例如只运行test_addition # pytest.main(['-k', 'addition']) pytest.main() ``` ### 相关问题 1. 如何指定运行单个测试用例? 2. 如何生成pytest测试报告? 3. 如何忽略某些测试用例? 通过以上步骤,你可以使用`pytest.main()`来运行测试,并根据需要传递参数来控制测试行为。</think>### 如何使用 pytest.main() 运行测试 `pytest.main()` 是 Pytest 框架的核心入口函数,用于在 Python 脚本中直接启动测试执行。以下是具体使用方法: #### 1. **基本用法** ```python import pytest def test_example(): assert 1 + 2 == 3 if __name__ == "__main__": pytest.main() # 执行当前文件所有测试 ``` - 自动发现并运行所有以 `test_` 开头的函数 - 输出详细测试报告(通过/失败用例统计)[^1] #### 2. **参数化运行** 通过列表传递命令行参数: ```python pytest.main([ "-v", # 显示详细输出 "--tb=short", # 简化错误跟踪 "tests/", # 指定测试目录 "-k", "login" # 只运行包含"login"的用例 ]) ``` #### 3. **指定运行范围** ```python pytest.main([ "test_api.py", # 运行单个文件 "test_db.py::TestConnection", # 运行特定测试类 "test_ui.py::test_button_click" # 运行单个测试方法 ]) ``` #### 4. **解决参数不生效问题** 若参数未生效: 1. 检查 IDE 运行配置,确保未覆盖参数 2. 显式设置执行器: ```python pytest.main(["-c", "pytest.ini"]) # 强制加载配置文件 ``` 3. 在调用前添加环境变量: ```python import os os.environ["PYTEST_ADDOPTS"] = "--verbose" # 预置参数 pytest.main() ``` [^3] #### 5. **完整示例** ```python import pytest # 测试用例 def test_addition(): assert 1 + 1 == 2 class TestSystem: def test_memory(self): assert 1024 * 1024 == 1048576 if __name__ == "__main__": # 带参数执行:显示详细报告,仅运行当前文件 pytest.main(["-v", __file__]) ``` ### 关键注意事项 1. **执行位置**:`pytest.main()` 会从**调用位置**递归发现测试用例 2. **返回值**:返回退出码(0表示全部通过) 3. **配置继承**:自动加载项目根目录的 `pytest.ini` 或 `conftest.py` 4. **线程安全**:避免在多线程中直接调用,建议用 `subprocess` 模块[^2] > 经验提示:在持续集成(CI)环境中,推荐使用 `pytest [参数]` 命令行方式;在调试/开发时再用 `pytest.main()` 脚本集成[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值