在n个数中找第k小的数(快选算法模板y神学习)

#include<iostream>

using namespace std;

const int N = 100010;

int n,k;
int q[N];

int quick_sort(int l,int r,int k)
{
	if(l == r) return q[l];
	int x = q[l],i=l-1,j=r+1;
	while(i<j)
	{
		while(q[++i]<x);
		while(q[--j]>x);
		if(i<j)swap(q[i],q[j]);
	}
	int sl = j - l + 1;
	if(k<=sl) return quick_sort(l,j,k);
	return quick_sort(j+1,r,k-sl);
}

int main(){
	cin>>n>>k;
	for(int i = 0;i < n;i++) cin>>q[i];
	cout<<quick_sort(0,n-1,k)<<endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要准备一个包含手写字的图像库。这里我们使用MNIST据集,它包含了许多手写字的图像和对应的标签。可以在网上下载到这个数据集。 接下来,我们需要编写一个模板匹配的算法,用于在输入图像字的位置。这里我们采用最简单的方法——基于平均灰度值的模板匹配。 具体步骤如下: 1. 读取图像库和待识别的图像; 2. 对于每个数字,计算其平均灰度值,作为该字的模板; 3. 对于待识别的图像,使用滑动窗口的方式在图像搜索字的位置; 4. 对于每个搜索到的位置,计算该位置的灰度值与每个数模板的匹配度; 5. 取匹配度最高的字作为该位置的识别结果。 下面是完整的 MATLAB 代码: ```matlab % 读取图像库和待识别的图像 train_images = loadMNISTImages('train-images.idx3-ubyte'); train_labels = loadMNISTLabels('train-labels.idx1-ubyte'); test_images = loadMNISTImages('t10k-images.idx3-ubyte'); test_labels = loadMNISTLabels('t10k-labels.idx1-ubyte'); % 定义模板大小 template_size = 28; % 计算每个数字的平均灰度值,作为该字的模板 templates = zeros(template_size, template_size, 10); for i = 0:9 idx = (train_labels == i); digit_images = train_images(:, idx); digit_mean = mean(digit_images, 2); templates(:, :, i+1) = reshape(digit_mean, template_size, template_size); end % 在待识别图像搜索字的位置,并进行识别 for i = 1:size(test_images, 2) image = reshape(test_images(:, i), 28, 28); best_match = -inf; best_digit = -1; for j = 0:9 template = templates(:, :, j+1); for x = 1:(size(image, 1)-template_size+1) for y = 1:(size(image, 2)-template_size+1) patch = image(x:(x+template_size-1), y:(y+template_size-1)); match = sum(sum(patch .* template)); if match > best_match best_match = match; best_digit = j; end end end end fprintf('Image %d: Predicted digit is %d\n', i, best_digit); end ``` 需要注意的是,这个算法的精度比较低,只能在简单的场景下进行识别。如果需要更高的精度,可以采用更复杂的算法,比如基于卷积经网络的深度学习算法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值