特殊的质数肋骨(C++实现)

题目描述
农民约翰母牛总是产生最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数。

例如有四根肋骨的数字分别是:7 3 3 1,那么全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。7331 被叫做长度 4 的特殊质数。

写一个程序对给定的肋骨的数目 N (1< =N< =8),求出所有的特殊质数。数字1不被看作一个质数。
输入
单独的一行包含N。
输出
按顺序输出长度为 N 的特殊质数,每行一个。
样例输入

4

样例输出

2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393

以上是题目

题目难点:

  • 判断素数
  • 时间复杂度
  • 递归链

题目分析:

  • 在之前的自己分析的过程中,判断素数最直观的方法就是直接循环遍历,从头到尾,但是如果对于一个很大的n来讲,这会严重影响效率,可能在OJ上跑不过。进一步优化就是在这个理论上:一个数的最大质数不超过其1/2;
    更多的优化参考:判断一个数是否是质数

  • 递归链的问题是递归的精华。我一开始想直接暴力推,但是时间复杂度的问题限制了我的想象。因为我在这个OJ上遇到的这题是归类为递归类型,所以递归应该是一种优质解。

  • 根据n=4的情况来看输出,发现第一个数一定是2,3,5,7四种,后面的数则在1,3,5,7,9中产生。这个有点类似于发现规律,难点也在这。因为如果除了2这个偶数,再出现偶数,那么这个数一定能被2整除,不构成质数。所以除了最高位的数字中有2的出现,其他位次的数绝对不可能出现2的倍数。

  • 那么再看其他位次,同理,要满足题目要求,必须去掉最低位的数字之后,剩下的数字也是质数,所以对应的ABC这样一个数的C不能为2的倍数。

代码如下:

#include<iostream>
#include<cmath>
using namespace std;
bool judge(int n)//判断素数 
{
	for(int i=2;i*i<=n;i++)//最大质数不超过其本身的二分之一 
	{
		if(n%i==0)return false;
	}
	return true;
}
void dg(int i,int n)//递归程序 
{
	if(n==1)//终止条件:如果当前这个数是10以内的,判断是否是质数即可判断输出与否 
	{
		if(judge(i))cout<<i<<endl;
		return ;
	}
	if(judge(i))//对当前位次的数是质数的情况下 ,分别对它扩大十倍+1,3,5,7,9五种情况进行递归判断输出 
	{
		dg(i*10+1,n-1);
		dg(i*10+3,n-1);
		dg(i*10+5,n-1);
		dg(i*10+7,n-1);
		dg(i*10+9,n-1);
	}
}
int main()
{
	int n;
	cin>>n;
	dg(2,n);//由于最高位是2,3,5,7的一种,所以可以直接调用递归程序,对2,3,5,7开头的程序进行判断 
	dg(3,n);
	dg(5,n);
	dg(7,n);
}


谨以此博客记录我的部分刷题历程
其中算法思想部分参考网上方法

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用ITK C++实现CT肋骨软骨的自动分割的代码实现,需要注意的是,实际应用中需要根据具体的数据及实际需求进行参数调整和算法优化。 ```c++ #include "itkImage.h" #include "itkImageFileReader.h" #include "itkBinaryThresholdImageFilter.h" #include "itkBinaryDilateImageFilter.h" #include "itkBinaryErodeImageFilter.h" #include "itkBinaryBallStructuringElement.h" #include "itkConnectedComponentImageFilter.h" #include "itkRelabelComponentImageFilter.h" #include "itkMaskImageFilter.h" #include "itkNeighborhoodIterator.h" #include "itkLabelContourImageFilter.h" #include "itkDiscreteGaussianImageFilter.h" using ImageType = itk::Image<unsigned char, 3>; using ReaderType = itk::ImageFileReader<ImageType>; using ThresholdFilterType = itk::BinaryThresholdImageFilter<ImageType, ImageType>; using DilateFilterType = itk::BinaryDilateImageFilter<ImageType, ImageType, itk::BinaryBallStructuringElement<ImageType::PixelType, ImageType::ImageDimension>>; using ErodeFilterType = itk::BinaryErodeImageFilter<ImageType, ImageType, itk::BinaryBallStructuringElement<ImageType::PixelType, ImageType::ImageDimension>>; using ConnectedComponentFilterType = itk::ConnectedComponentImageFilter<ImageType, ImageType>; using RelabelFilterType = itk::RelabelComponentImageFilter<ImageType, ImageType>; using MaskFilterType = itk::MaskImageFilter<ImageType, ImageType>; using IteratorType = itk::NeighborhoodIterator<ImageType>; using ContourFilterType = itk::LabelContourImageFilter<ImageType, ImageType>; using GaussianFilterType = itk::DiscreteGaussianImageFilter<ImageType, ImageType>; int main(int argc, char* argv[]) { // 1. 读取CT图像 ReaderType::Pointer reader = ReaderType::New(); reader->SetFileName("CT_image.nii.gz"); reader->Update(); ImageType::Pointer image = reader->GetOutput(); // 2. 预处理 GaussianFilterType::Pointer gaussianFilter = GaussianFilterType::New(); gaussianFilter->SetInput(image); gaussianFilter->SetVariance(2.0); gaussianFilter->Update(); ThresholdFilterType::Pointer thresholdFilter = ThresholdFilterType::New(); thresholdFilter->SetInput(gaussianFilter->GetOutput()); thresholdFilter->SetLowerThreshold(-500); // 软骨的阈值范围为-1000到-500 thresholdFilter->SetUpperThreshold(-1000); thresholdFilter->SetInsideValue(255); thresholdFilter->SetOutsideValue(0); thresholdFilter->Update(); // 3. 软骨分割 DilateFilterType::Pointer dilateFilter = DilateFilterType::New(); dilateFilter->SetInput(thresholdFilter->GetOutput()); dilateFilter->SetKernelRadius(5); dilateFilter->Update(); ErodeFilterType::Pointer erodeFilter = ErodeFilterType::New(); erodeFilter->SetInput(dilateFilter->GetOutput()); erodeFilter->SetKernelRadius(15); erodeFilter->Update(); ContourFilterType::Pointer contourFilter = ContourFilterType::New(); contourFilter->SetInput(erodeFilter->GetOutput()); contourFilter->SetFullyConnected(true); contourFilter->SetBackgroundValue(0); contourFilter->SetForegroundValue(255); contourFilter->Update(); ImageType::Pointer cartilageImage = contourFilter->GetOutput(); // 4. 肋骨分割 DilateFilterType::Pointer ribDilateFilter = DilateFilterType::New(); ribDilateFilter->SetInput(thresholdFilter->GetOutput()); ribDilateFilter->SetKernelRadius(3); ribDilateFilter->Update(); ConnectedComponentFilterType::Pointer ccFilter = ConnectedComponentFilterType::New(); ccFilter->SetInput(ribDilateFilter->GetOutput()); ccFilter->Update(); RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New(); relabelFilter->SetInput(ccFilter->GetOutput()); relabelFilter->SetMinimumObjectSize(10000); relabelFilter->Update(); MaskFilterType::Pointer maskFilter = MaskFilterType::New(); maskFilter->SetInput(image); maskFilter->SetMaskImage(relabelFilter->GetOutput()); maskFilter->Update(); IteratorType::RadiusType radius; radius.Fill(1); IteratorType it(radius, image, image->GetLargestPossibleRegion()); it.GoToBegin(); while (!it.IsAtEnd()) { ImageType::IndexType idx = it.GetIndex(); if (maskFilter->GetOutput()->GetPixel(idx) == 0) { it.Set(0); } ++it; } ImageType::Pointer ribImage = it.GetImage(); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

澄澈i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值