# matlab

Matlab:函数返回多个值

在 MATLAB 中,函数可以返回多个值。要返回多个值,可以使用中括号将它们括起来,并用逗号分隔。以下是一个简单的示例:

1
2
3
4
5
function [output1, output2] = myFunction(input1, input2)
% function code here
output1 = someValue1;
output2 = someValue2;
end

在这个例子中,函数 myFunction 接受两个输入参数 input1input2,并返回两个输出值 output1output2。在函数的结尾处,使用 output1output2 变量来存储要返回的值。

要调用这个函数并获取返回的值,可以使用以下语法:

1
[output1, output2] = myFunction(input1, input2);

这将调用函数 myFunction 并将输入参数 input1input2 传递给它。函数将计算输出值 output1output2,并将它们分别赋值给变量 output1output2

相关问题

在 Matlab 中,可以通过在函数定义行中使用多个输入参数来实现函数接受多个输入。以下是一个简单的示例:

1
2
3
4
function output = myFunc(input1, input2, input3)
% 在函数体内处理输入参数
output = input1 + input2 + input3;
end
阅读全文 »

# matlab

Matlab:在函数里使用“全局变量”

函数里的变量自在函数内有效, 那么如何在函数内调用外部的变量呢?

  1. 全局变量 global
    global可以定义全局变量, 但要注意先定义, 再赋值. 否则会有警告:

Warning: The value of local variables may have been changed to match the globals. Future versions of MATLAB will require that you declare a variable to be global before you use that variable.

并且需要在你写的函数里面声明使用全局变量。

1
2
3
4
5
6
7
8
9
10
11
12
global x
x=5;

subfunc % 输出为5

function output=subfunc
global x % 在使用全局变量时, 要先声明
output=x;
end

clear global x % 清除全局变量

  1. 使用子函数

子函数默认可以调用其母函数中所有的变量

1
2
3
4
5
6
7
8
9
function main
x=5;
subfunc

function output=subfunc

output=x;
end
end

执行 main, 输出5. 子函数共享父函数的变量

阅读全文 »

# matlab

matlab:三角函数的运用

matlab里三角函数有sin,cos,tan,csc,sec和cot,是弧度制;
如,sin(pi/2)=1;

sind,cosd,tand是角度制;
如,sind(90)=1;

asin,acos,atan是弧度制反三角函数;

asind,acosd,atand是角度制反三角函数;

sinh,cosh,tanh是双曲函数

阅读全文 »

# C++
# 未完成

目录

1、vector对象的定义和初始化方式 2、vector 常用基础操作 3、使用迭代器的遍历、插入、删除操作

4、vector 元素的重排操作(排序、逆序等)

5、vector 中找最值

6、改变vector大小 及其 内存分配机制

7、vector数组 与 内置数组 的选择问题


1、vector对象的定义和初始化方式

常用的初始化方式及作用如下:

vector 中的数据类型 T 可以代表任何数据类型,如 int、string、class、vector(构建多维数组) 等,就像一个可以放下任何东西的容器,因此 vector 也常被称作容器。字符串类型 string 也是一种容器,c++ 中的不同种类的容器拥有很多相同的操作,因此 string 的很多操作方法可以直接用在 vector 中。

vector<T> v1 v1 是一个元素类型为 T 的空 vector
vector<T> v2(v1) 使用 v2 中所有元素初始化 v1
vector<T> v2 = v1 同上
vector<T> v3(n, val) v3 中包含了 n 个值为 val 的元素
vector<T> v4(n) v3 中包含了 n 个默认值初始化的元素
vector<T> v5{a, b, c...} 使用 a, b, c... 初始化 v5
vector<T> v1 同上

vector<vector<int>> matrix(M,vector<int>(N));

二维数组初始化

2、vector 常用基础操作

 下表列出了 添加元素、查询、索引、赋值、比较 等常用操作方法。

v.empty() 如果 v 为空则返回 true,否则返回 false
v.size() 返回 v 中元素的个数
v.push_back(val)

向 vector 的尾端添加值为 val 的元素。

注意:vector 不支持 push_front 操作。

v.pop_back(val)

删除尾元素,返回void。vector同样 不支持 pop_front 操作。若想在同时弹出元素的值,就必须在执行弹出之前保存它(可以使用 v.back())。

v[n] 返回 v 中第 n 个位置上元素的引用,不能用下标操作添加元素
v.back() 返回 v 中最后一个元素的引用
v.front() 返回 v 中第一个元素的引用
v1 = v2 用 v2 中的元素替换 v1 中的元素
v1 = {a, b, c...} 用元素 {a, b, c...} 替换 v1 中的元素
v1 == v2 当且仅当拥有相同数量且相同位置上值相同的元素时,v1 与 v2 相等
v1 != v2 自行体会
<, <=, >, >= 以字典序进行比较

3、使用迭代器的遍历、插入、删除操作

迭代器类似于指针,提供了对象的间接访问,但获取迭代器并不是使用取地址符。如果将指针理解为元素的“地址”,那么迭代器可以理解为元素的“位置”。可以使用迭代器访问某个元素,迭代器也能从一个元素移动到另一个元素。

一个迭代器的范围由一对迭代器表示,分别为 beginend。其中 begin 成员返回指向第一个元素的迭代器;end 成员返回容器最后一个元素的下一个位置(one past the end),也就是指向一个根本不存在的尾后位置,这样的迭代器没什么实际含义,仅是个标记而已,表示已经处理完了容器中的所有元素。所以 beginend 表示的是一个左闭右开的区间 [ beginend)

迭代器可以用来实现容器的遍历插入等操作,可以细品下面的例子:

1、遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void){
vector<string> a{"0", "1", "2", "3", "4", "5", "6", "7", "8"};
auto it = a.begin();
// 返回一个迭代器类型,一般来说我们并不关心迭代器具体的数据类型
while(it != a.end()) {
cout << *it << " ";
it++;
}
return 0;
}
// 运行结果
//0 1 2 3 4 5 6 7 8

2、插入

插入操作的函数:

v.insert(p, n, val) :在迭代器 p 之前插入 n 个值为 val 的元素,返回新添加的第一个元素的迭代器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(void){ vector<int> a{1, 2, 3, };
auto it1 = a.begin();

// 返回一个迭代器类型,一般来说我们并不关心迭代器具体的数据类型

auto it2 = a.insert((it1+1), {6, 7, 8});
// 利用迭代器在第二个元素之前插入数据

cout << *it2 << endl;
// 返回的是新插入元素第一个元素的迭代器

auto it = a.begin();
while(it != a.end()) {
cout << *it << " ";
it++;
} return 0;}
// 输出结果
//61 6 7 8 2 3

3、删除 

删除操作的函数:

v.erase(p) :删除迭代器 p 所指的元素,返回指向被删除元素之后元素的迭代器。

v.erase(b, e) :删除迭代器 b, e 之间的元素,返回指向最后一个被删除元素之后元素的迭代器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(void){ vector<int> a{1, 2, 3, };
auto it1 = a.begin();
// 返回一个迭代器类型,一般来说我们并不关心迭代器具体的数据类型

auto it2 = a.erase(it1+1);
// 删除元素 2

cout << *it2 << endl;
// 返回的是新插入元素第一个元素的迭代器

auto it = a.begin();
//
while(it != a.end()) {
cout << *it << " ";
it++;
}
return 0;
}

// 运行结果

//31 3

4、vector 元素的重排操作(排序、逆序等)

容器的重排需要用到头文件 <algorithm> 中的算法

1、排序 sort()

使用到的函数为 sort() :按输入序列的字典序升序排序,原位操作,无返回值函数原型:

1
void std::sort<std::vector<int>::iterator>(std::vector<int>::iterator, std::vector<int>::iterator)

举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
vector<int> a{2, 0, 2, 2, 0, 3, 0, 9};
sort(a.begin(), a.end());
//原位操作
for(int i:a)
cout << i << " ";
return 0;}
// 输出结果
//0 0 0 2 2 2 3 9

2、消除相邻的重复元素 unique()

使用到的函数为 unique() :将输入序列相邻的重复项“消除”,返回一个指向不重复值范围末尾的迭代器,一般配合 sort() 使用,函数原型:

1
2
std::vector<int>::iterator 
std::unique<std::vector<int>::iterator>(std::vector<int>::iterator, std::vector<int>::iterator)

举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){ vector<int> a{2, 0, 2, 2, 0, 3, 0, 9};
sort(a.begin(), a.end());
// 先排序
for(int i:a) cout << i << " ";
// 输出
cout << endl;
auto end_unique = unique(a.begin(), a.end());
//将输入序列相邻的重复项“消除”,返回一个指向不重复值范围末尾的迭代器 a.erase(end_unique, a.end());
// 删除末尾元素
for(int i:a) cout << i << " ";
// 输出
return 0;
}
// 运行结果
//0 0 0 2 2 2 3 9 0 2 3 9

3、逆序 reverse()

方法1:使用到的函数为 reverse() :将输入序列按照下标逆序排列,原位操作,无返回值函数原型:

1
void std::reverse<std::vector<int>::iterator>(std::vector<int>::iterator, std::vector<int>::iterator)

 方法2:使用 greater<int>() 作为参数(内置函数)

1
sort(nums.begin(), nums.end(), greater<int>());

 举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
vector<int> a{2, 0, 2, 2, 0, 3, 0, 9};
reverse(a.begin(), a.end());
// 原位逆序排列

for(int i:a)
cout << i << " ";
// 输出

return 0;
}
// 运行结果
//9 0 3 0 2 2 0 2

5、vector 中找最值

容器的重排同样需要用到头文件 <algorithm> 中的算法。

1、最大值 auto it = max_element(v.begin, v,end()),返回最大值的迭代器,函数原型如下:

1
constexpr std::vector<int>::iterator std::max_element<std::vector<int>::iterator>(std::vector<int>::iterator, std::vector<int>::iterator)

2、最小值 auto it = min_element(v.begin, v,end()),返回最小值的迭代器,函数原型如下:

1
constexpr std::vector<int>::iterator std::min_element<std::vector<int>::iterator>(std::vector<int>::iterator, std::vector<int>::iterator)

3、相对位置大小 auto b = distance(x, y),x、y 是迭代器类型,返回 x、y 之间的距离,可以用来获取最大/小值的索引,函数原型如下:

1
std::ptrdiff_t std::distance<std::vector<int>::iterator>(std::vector<int>::iterator __first, std::vector<int>::iterator __last)

举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void){
vector<int> a({0,1,-2,3});

auto b = distance(a.begin(), min_element(a.begin(), a.end()));

cout << a[b] << endl;

return 0;}
// 输出
//-2

6、改变vector大小 及其 内存分配机制

与内置数组一样,vector 的所有元素必须存放在一片连续的内存中,但 vector 的大小可变性使得其所占用的内存大小也是可变的。

为了避免每次改变 vector 时重新分配内存空间再将原来的数据从新拷贝到新空间的操作,标准库实现者采用了减少容器空间重新分配次数的策略:当不得不获取新空间时,vector(string 也是如此)通常会分配比需求更大的空间作为预留的备用空间,这样就减少了重新分配空间的次数。

  • 改变 vector 的大小可以使用 v.resize(n, t) 函数,调整 v 的大小为 n 个元素,任何新添加的元素都初始化为值 t

举例:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
using namespace std;

int main(void){
vector<vector<int>> a;
a.resize(3, vector<int>(3));
cout << "row : " << a.size() << endl;
cout << "col : " << a[0].size() << endl;
return 0;}
// 输出
//row : 3col : 3
  • 函数 v.resize(n)  可以用来告知容器分配至少能分配 n 个元素的内存空间。并不改变容器中元素的数量,仅影响 vector 预先分配多大的内存空间

7、vector数组 与 内置数组 的选择问题

一般来说,我们在使用 C++ 编程时会将 vector 类型的数据与类似于使用 a[N] 定义的内置数组统称为数组,两者是很类似的数据结构,在一般的任务中使用 vector数组 与使用内置数组通常没有什么区别。两者的区别主要如下:

  • vector数组 是 C++ 的标准库类型,即使用 vector 定义的变量本质上是定义了一个 vector 类的对象。而类似于使用 a[N] 定义的数组是内置数组,类似于 int、float 等内置类型的变量。
  • vector数组 的大小可变,而内置数组类型在定义时必须明确定义大小,之后大小不能变化。因为内置数组的大小固定,因此对某些特殊的应用来说程序运行时的性能较好,但是也失去了一定的灵活性。

如果不确定元素的确定个数,那么 vector 就是最好的选择。

# C++
[[___未完成]]

C++返回vector、将vector作为参数传递

在C++里很多时候我们会遇到函数想返回两个以上结果的情况,这时候可以用数组(vector)、类来作为容器返回,也可以声明一个全局变量的数组,将数值存放在数组里解决。

第一个方式是使用引用来解决,将vector的引用在函数间传递 这是一个例子,假设我要传入一个数,我的函数的功能是返回这个数后面十个数的序列。

#include #include using namespace std;

/ 输入一个数,返回这个数后面的十个数字序列 注意参数的这个 & 符号不能省略 / void getSequence(int num,vector& sequence){ for(int i=0;i<10;i++){ sequence.push_back(i+num); } }

int main(){ int num=9; vector sequence; //在主调函数这边,直接传入该vector变量 getSequence(num,sequence); //访问该vector的值的时候,也是直接访问即可 for(vector::iterator it=sequence.begin();it!=sequence.end();it++){ cout<<*it<<endl; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

第二个方式是返回vector变量 在被调用函数中声明一个vector变量,函数结束的时候返回vector变量 但是这样的传参方式我有一个不太理解的地方,既然vector变量是在被调函数中声明的,就应该是一个局部变量,在被调函数执行完毕之后这部分空间应该会被销毁,这个变量就无法访问到了,莫非vector是在堆空间开辟的地址?然后返回的其实是指向堆空间vector的指针?

vector getSequence(int num){ vector sequence; for(int i=0;i<10;i++){ sequence.push_back(i+num); } return sequence; }

int main(){ int num=9; vector sequence; //在主调函数这边,只传入num sequence=getSequence(num); //访问该vector的值的时候,也是直接访问即可 for(vector::iterator it=sequence.begin();it!=sequence.end();it++){ cout<<*it<<endl; } }

阅读全文 »

# C++

C ++字符串与浮点数和双浮点数转换

将字符串转换为浮点数的最简单方法是使用以下C ++ 11函数:

  • std :: stof() - 将string转换为float
  • std :: stod() - 将string转换为double
  • std :: stold() - 将string转换为long double。

这些函数在string头文件中定义。

示例1:C ++字符串转换为浮点和双浮点数

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include <string>

int main() {
    std::string str = "123.4567";

    // 将字符串转换为浮点数
    float num_float = std::stof(str);

    // 将字符串转换为双浮点数 double
    double num_double = std::stod(str);

   std:: cout<< "num_float = " << num_float << std::endl;
   std:: cout<< "num_double = " << num_double << std::endl;

    return 0;
}
输出结果

num_float = 123.457 num_double = 123.457

示例2:将C ++ char数组转换为double

我们可以使用std::atof()函数将char数组转换为double。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

// atoi()需要cstdlib
#include <cstdlib>

int main() {

    // 声明和初始化字符数组
    char str[] = "123.4567";

    double num_double = std::atof(str);

    std::cout << "num_double = " << num_double << std::endl;
    
    return 0;
}
输出结果

num_double = 123.457

阅读全文 »

# C++

前言

字符串操作是各种算法题中的常客,很多数据常常以字符串形式给出,其中有的需要自己转化成整数,而一些整型数据有时转换成字符串处理起来更加方便,比如判断一个整数是否是回文数,所以字符串和整数的转换是一些问题处理的基础步骤,C++ 在处理这类问题时并不像 Python 那样方便,但是也有许多方法能够实现,为了今后查找方便,整理如下。

int 转 string

通过 std::to_string() 函数转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>

int main()
{
int num = 123;
std::cout << std::to_string(num);
return 0;
}

这种方式在 C++11 中才能使用,编译时记得加上 --std=c++11 的选项

## 通过 ostringstream 转换

#include <iostream>
#include <sstream>

int main()
{
int num = 123;
std::ostringstream ss;
ss << num;
std::cout << ss.str();
return 0;
}

这是一种通过字符流的方式将整数转换成字符串,这种方式在C++11之前也可以使用

通过 sprintf 转换

1
2
3
4
5
6
7
8
9
10
11
12

#include <stdio.h>

int main()
{
int num = 123;
char buffer[256];
sprintf(buffer, "%d", num);

printf("%s", buffer);
return 0;
}

这是一种C语言中的转换方式,sprintf 也可以换成更安全的 snprintf 函数

string 转 int

通过 istringstream 转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
#include <sstream>

int main()
{
std::string str = "668";
int num = 0;

std::istringstream ss(str);
ss >> num;

std::cout << num;
return 0;
}

使用 istringstream 可以从字符流中读取整数,与 ostringstream 是一种相反的操作

使用 sscanf 来转化

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
#include <stdio.h>

int main()
{
std::string str = "668";
int num = 0;

sscanf(str.c_str(), "%d", &num);
std::cout << num;
return 0;
}

注意 sscanf 函数的第一个参数类型是 const char *,string类型的参数需要转换一下

使用 atoi 转换

1
2
3
4
5
6
7
8
9
10

#include <iostream>
#include <stdlib.h>

int main()
{
std::string str = "668";
std::cout << atoi(str.c_str());
return 0;
}

atoi 函数的头文件是 stdlib.h,同样是一个C语言中的函数

使用方法stod

该方法在C++11中被支持,十分地方便,但是需要确保给定的string是否可以转化为double,不然会抛出错误。

总结 itoa 不是c语言标准函数,在跨平台的整数转字符串的代码中不要使用这个函数 atoi 是一个标准函数,需要将它和 itoa 区别开来,这一点很容易记混的 如果是在C++环境中进行转换,推荐使用 stringstream 字符流的形式和 to_string 函数

阅读全文 »

[[___C++]]

C++:string的运用和相关方法

一、删除std::string或std::wstring的最后一个字符:

s.pop_back();

  从s中移走最后一个元素。在string/wstring中相当于移走最后一个char/wchar_t。这个方法算是比较简单的了。

s.erase(s.end()-1);

  删除s的最后一个字符

s=s.substr(0,s.length()-1);

  取出s从最开始到倒数第二个字符之间的字符串,赋值给s。相当于去掉最后一个字符

阅读全文 »

# C++
[[___未完成]]

1.概念

  运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能。这个函数叫做运算符重载函数(常为类的成员函数)。
  用函数的方式实现了(+ - * / []数组 && || 逻辑 等)运算符的重载。根据需求决定重载那些运算符,用到的时候再百度案例即可。

阅读全文 »

# 未完成

01背包问题详解

01背包是一种动态规划问题。动态规划的核心就是状态转移方程,本文主要解释01背包状态转移方程的原理。

问题描述

01背包问题可描述为如下问题: 有一个容量为V的背包,还有n个物体。现在忽略物体实际几何形状,我们认为只要背包的剩余容量大于等于物体体积,那就可以装进背包里。每个物体都有两个属性,即体积w和价值v。 问:如何向背包装物体才能使背包中物体的总价值最大?

为什么不用贪心?

我在第一次做这个题目时考虑的是贪心算法。所谓贪心问题,就是每一步决策都采取最优解,按照此方案最后结果也是最优解。 为什么这个问题不能用贪心呢? 举个例子 我的背包容量为10,而且有4个物体,它们的体积和价值分别为
w1 = 8, v1 = 9 w2 = 3, v2 = 3 w3 = 4, v3 = 4 w4 = 3, v4 = 3 贪心是每一步采取最优拿法,即每一次都优先拿价值与体积比值最大的物体
c1 = v1/w1 = 1.125(最大) c2 = v2/w2 = 1 c3 = v3/w3 = 1 c4 = v4/w4 = 1 所以优先拿第一个物体,随后背包再也装不下其他物体了,则最大价值为9。 但是这个问题的最优解是取物体2,3,4装进背包,最大价值为3+4+3=10!!! 所以这个问题不可以用贪心法来处理。

原始的 01背包

01背包的状态转移方程为 `f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[j])

i代表对i件物体做决策,有两种方式—放入背包和不放入背包。 j表示当前背包剩余的容量。

转移方程的解释: 创建一个状态矩阵f,横坐标 i 是物体编号,纵坐标 j 为背包容量。 首先将 f 第0行和第0列初始化为0 (代码里面将整个f初始化为0了,其实只初始化第0行和第0列就够了)。这个表示不放物体时最大价值为0 。(物体编号从1开始) 接下来依次遍历f的每一行。如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 1; i <= n; i++)
{
for (int j = V; j >= 0; j--)
{
if (j >= w[i])//如果背包装得下当前的物体
{
f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]);
}
else//如果背包装不下当前物体
{
f[i][j] = f[i - 1][j];
}
}
}

如果背包装得下当前的物体,在遍历过程中分别计算第i件物体放入和不放入背包的价值,取其中大的做为当前的最大价值。 如果背包装不下当前物体那么第i个物体只有不放入背包一种选择。

不放入背包时:第i次决策后的最大价值和第i-1次决策时候的价值是一样的(还是原来的那些物体,没多没少)。 放入背包时:第i次决策后的价值为 第i-1次决策时候的价值 加上 当前物体的价值v[j]。物体放入背包后会使背包容量变为 j ,即没放物体之前背包的容量为j - w[i]。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
using namespace std;
#define max(N1,N2) N1>N2?N1:N2
int main()
{
/*
第一行输入背包容量V和物体的个数n
接下来有n行,每行包含两个数字,分别为该物体的花费和价值
*/
vector<int> w, v;//w为花费,v为价值
vector<vector<int>> f;//f状态矩阵
int V, n;//V背包容量,n物体数
while (cin >> V >> n)
{
w.clear();
v.clear();
f.clear();
w.push_back(0);
v.push_back(0);

//输入原始数据
for (int i = 1; i <= n; i++)
{
int cur_w, cur_v;
cin >> cur_w >> cur_v;
w.push_back(cur_w);
v.push_back(cur_v);
}

//初始化状态矩阵
for (int i = 0; i <= n; i++)
{
vector<int> buff(V + 1, 0);
f.push_back(buff);
}

//动态规划过程
for (int i = 1; i <= n; i++)
{
for (int j = V; j >= 0; j--)
{
if (j >= w[i])
{
f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]);
}
else
{
f[i][j] = f[i - 1][j];
}
}
}

//输出答案
int ans = f[n][V];
cout << ans << endl;
}
return 0;
}
阅读全文 »
0%