笔试题目python

人间小客 2022-08-06 05:27:12 阅读数:92

Python题目笔试试题

知识点一,装饰器的使用

源自于,牛客网的python机器学习的算法笔试题目

装饰器

基本的语法,@糖标志,多个装饰器从下到上依次进行,先调用最原始的函数,再调用装饰器中的内容

from tkinter import N
def dec(f):
n = 3
def wrapper(*args,**kw):
return f(*args,**kw)* n
return wrapper
@dec
def foo(n):
return n*2
print(foo(2))

//暴力法解决问题,O(2)
#include <iostream>
#include <vector>
using namespace std;
//算法一
int max_value(vector<int> &nums,int i,int j){

for(int k = i;k<j;k++){

if(nums[k]<=nums[k+1]){

swap(nums[k],nums[k+1]);
}
}
return nums[i];
}
void get_abs_max(int n,vector<int> &nums){

int max1 = 0;
int max2 = 0;
int max3 = 0;
int result = 0;
for(int k1 = 0; k1<=n-2;k1++){

max1 = max_value(nums,0,k1);
max2 = max_value(nums,k1+1,n-1);
max3 = max1>max2?(max1-max2):(max2-max1);
if(result < max3){

result = max3;
}
}
cout<<result<<endl;
}
//算法二真妙呀
void ks_solve(int n,vector<int>&nums){

int max1 = 0;
for (int i = 0; i < nums.size(); i++) {

max1 = nums[i]>max1? nums[i]:max1;
}
cout<<max1 - min(nums[0], nums[nums.size() - 1]);
}
int main(){

int n;
cin>>n;
vector<int> nums(n);
for(int i = 0;i<n;i++){

cin>>nums[i];
}
get_abs_max(n,nums);
cout<<"啊啊啊啊真秒呀"<<endl;
ks_solve(n,nums);
return 0;
}
版权声明:本文为[人间小客]所创,转载请带上原文链接,感谢。 https://blog.csdn.net/Android_WPF/article/details/125685214