现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度。如果左右有等距离长度素数,则输出左侧的值及相应距离。 如果输入的整数本身就是素数,则输出该素数本身,距离输出0...

本文介绍了一个用于查找距离任意整数最近的素数及其距离的C语言程序。该程序通过两个方向搜索最近的素数,并返回素数及其与输入整数的距离。同时,包含了一个判断素数的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <math.h>
int pro(int p);
int main() {
int a,n;
scanf("%d",&n);
while(n--) {
scanf("%d",&a);
pro(a);
}
}
int pro(int p) { //求距离最近的素数及距离
int b,c,d,k,m,n,sum1,sum2;
int prime(int x);
if(p==1) printf("2 1\n"); //输入为1时的输出
else {
if(prime(p)!=0) printf("%d 0\n",p); //是素数的输出
else { //其它不是素数的输出
b=p,c=p;
while(b--) {
if(prime(b)!=0) break;
}
m=b;
while(c++) {
if(prime(c)!=0) break;
}
n=c;
sum1=p-m;
sum2=n-p;
if(sum1<=sum2) k=sum1,d=m;
else k=sum2,d=n;
printf("%d %d\n",d,k);
}
}
}
int prime(int x) { //判断是否为素数
int i,j;
j=sqrt(x);
for(i=2;i<=j;i++){
if(x%i==0) x=0;
}
return x;
}

转载于:https://www.cnblogs.com/Tristan-Adams/p/8710540.html

using GridKey = std::tuple<int, int>; struct GridHash { size_t operator()(const GridKey& k) const { auto hash_int = std::hash<int>(); return hash_int(std::get<0>(k)) ^ (hash_int(std::get<1>(k)) << 1); } }; void buildSpatialGrid(std::map<tag_t, Point2d>& mapFaceCylPoint, // Point3d→Point2d std::unordered_map<GridKey, std::vector<tag_t>, GridHash>& gridMap, double cellSize) { gridMap.clear(); for (const auto& element : mapFaceCylPoint) { tag_t tag = element.first; const Point2d& point = element.second; // Point3d→Point2d int x = std::floor(point.X / cellSize); int y = std::floor(point.Y / cellSize); // 移除了z坐标计算 gridMap[{x, y}].push_back(tag); // 二维网格键 } } bool adjustWithGrid(std::map<tag_t, Point2d>& mapFaceCylPoint, // Point3d→Point2d double min_dist, int max_iters) { const double cell_size = min_dist; const double min_dist_sq = min_dist * min_dist; bool adjusted = false; for (int iter = 0; iter < max_iters; ++iter) { std::unordered_map<GridKey, std::vector<tag_t>, GridHash> gridMap; buildSpatialGrid(mapFaceCylPoint, gridMap, cell_size); bool current_adjusted = false; for (const auto& element : mapFaceCylPoint) { tag_t current_tag = element.first; Point2d current_point = element.second; // Point3d→Point2d int gx = std::floor(current_point.X / cell_size); int gy = std::floor(current_point.Y / cell_size); // 移除了gz坐标 // 二维相邻网格检测(3x3网格) for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { GridKey key(gx + dx, gy + dy); // 二维键 if (!gridMap.count(key)) continue; for (const auto& neighbor_tag : gridMap.at(key)) { if (neighbor_tag == current_tag) continue; if (neighbor_tag < current_tag) continue; auto& neighbor_point = mapFaceCylPoint.at(neighbor_tag); // 二维距离计算 double delta_x = neighbor_point.X - current_point.X; // 重命名避免变量名冲突 double delta_y = neighbor_point.Y - current_point.Y; double dist_sq = delta_x * delta_x + delta_y * delta_y; // 移除了dz if (dist_sq < min_dist_sq && dist_sq > 0) { // 调整逻辑需要删除Z分量处理... double dist = std::sqrt(dist_sq); double delta = (min_dist - dist) / dist; double move = 0.5 * delta; // 获取当前点与邻居点的引用 Point2d& current_point_ref = mapFaceCylPoint.at(current_tag); // 显式获取引用 Point2d& neighbor_point_ref = mapFaceCylPoint.at(neighbor_tag); // 纯二维平面移动(删除Z分量处理) current_point_ref.X -= delta_x * move; current_point_ref.Y -= delta_y * move; neighbor_point_ref.X += delta_x * move; neighbor_point_ref.Y += delta_y * move; current_adjusted = true; } } } } } if (!current_adjusted) break; adjusted = true; } return adjusted; } 在UG制图中以上代码不能检测出标注尺寸的重叠
最新发布
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值