7-8 python
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
n = int(input())
arr = []
for i in range(n):
x = int(input())
arr.append(x)
arr = quicksort(arr)
for index,a in enumerate(arr):
if index == len(arr) - 1:
print(f'{a}')
break
print(f'{a}', end=' ')
7-9 c++
#include
#include
int a[110];
int n;
void merge(int a[],int tem[],int p,int q,int r)
{
int i=p;int j=q+1;int pos=p;
while(i<=q&&j<=r){
if(a[i]
7-10 c++
#include
using namespace std;
const int N = 1010;
int f[N];
int v[N], w[N];
int n, m;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> m >> n;
for (int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i];
for (int i = 1; i <= n; i++) {
for (int j = m; j >= v[i]; j--) {
f[j] = max(f[j], f[j - v[i]] + w[i]);
}
}
cout << f[m] << endl;
}
7-11 python
def matrix_chain_order(p):
n = len(p) - 1
m = [[0 for x in range(n + 1)] for y in range(n + 1)]
for L in range(2, n + 1):
for i in range(1, n - L + 2):
j = i + L - 1
m[i][j] = float('inf')
for k in range(i, j):
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]
if q < m[i][j]:
m[i][j] = q
return m[1][n]
if __name__ == "__main__":
n = int(input())
dims = list(map(int, input().split()))
print(matrix_chain_order(dims))
7-12 c++
#include
#include
#include
using namespace std;
void LCS(string s1,string s2)
{
int m=s1.length()+1;
int n=s2.length()+1;
int **c;
int **b;
c=new int* [m];
b=new int* [m];
for(int i=0;i=c[i+1][j])
{
c[i+1][j+1]=c[i][j+1];
b[i+1][j+1]=2; //2表示箭头向 上
}
else
{
c[i+1][j+1]=c[i+1][j];
b[i+1][j+1]=3; //3表示箭头向 左
}
}
}
stack same; //存LCS字符
stack same1,same2; //存LCS字符在字符串1和字符串2中对应的下标,方便显示出来
for(int i = m-1,j = n-1;i >= 0 && j >= 0; )
{
if(b[i][j] == 1)
{
i--;
j--;
same.push(s1[i]);
same1.push(i);
same2.push(j);
}
else if(b[i][j] == 2)
i--;
else
j--;
}
for(int i=0;i>s1;
string s2;
cin>>s2;
LCS(s1,s2);
return 0;
}