当前位置: 首页 > wzjs >正文

酒店电子商务网站建设流程搜索引擎在线

酒店电子商务网站建设流程,搜索引擎在线,公司宣传册设计样本怎么排版,网站开发和浏览器兼容问题1 打印菱形 常规方法的核心思想是通过控制空格和星号(*)的数量来构造菱形的每一行。 上半部分:逐行增加星号数量,减少空格数量。下半部分:逐行减少星号数量,增加空格数量。 1.2 图解 假设我们要打印一个…

1 打印菱形

常规方法的核心思想是通过控制空格和星号(*)的数量来构造菱形的每一行。

  • 上半部分:逐行增加星号数量,减少空格数量。
  • 下半部分:逐行减少星号数量,增加空格数量。

1.2 图解

假设我们要打印一个边长为5的菱形:

    ****************
*************************
  • 第1行:4个空格 + 1个星号。
  • 第2行:3个空格 + 3个星号。
  • 第3行:2个空格 + 5个星号。
  • 第4行:1个空格 + 7个星号。
  • 第5行:0个空格 + 9个星号。
  • 第6行开始重复第4到第1行的过程。

1.3 实现代码(chat)

#include <iostream>
using namespace std;void printDiamond(int n) {// 打印上半部分for (int i = 1; i <= n; ++i) {// 打印空格for (int j = 1; j <= n - i; ++j) {cout << " ";}// 打印星号for (int j = 1; j <= 2 * i - 1; ++j) {cout << "*";}cout << endl;}// 打印下半部分for (int i = n - 1; i >= 1; --i) {// 打印空格for (int j = 1; j <= n - i; ++j) {cout << " ";}// 打印星号for (int j = 1; j <= 2 * i - 1; ++j) {cout << "*";}cout << endl;}
}int main() {int n;cout << "input:";cin >> n;printDiamond(n);return 0;
}

2 曼哈顿距离法打印菱形

曼哈顿距离法利用二维坐标系中的点到中心点的距离来判断是否打印星号。具体步骤如下:

  • 将菱形的中心设为原点 (0, 0)
  • 对于每个点 (x, y),如果其曼哈顿距离(即 ∣ x ∣ + ∣ y ∣ |x| + |y| x+y)小于等于给定的半径 r r r,则打印星号;否则打印空格。

曼哈顿距离公式为:
d = ∣ x ∣ + ∣ y ∣ d = |x| + |y| d=x+y

  • 中心点为 (0, 0)
  • 每个点的曼哈顿距离满足 ∣ x ∣ + ∣ y ∣ ≤ r |x| + |y| \leq r x+yr,其中 r = 4 r=4 r=4

cpp代码

#include <iostream>
using namespace std;
void solve(int n) {int r = n - 1; // 菱形的半径for (int y = -r; y <= r; ++y) {for (int x = -r; x <= r; ++x) {if (abs(x) + abs(y) <= r) {cout << "*";} else {cout << " ";}}cout << endl;}
}
int main() {int n;cin >> n;solve(n);return 0;
}
    ** **   **     *
*       **     **   ** **

打印空心菱形,只打印等于半径的位置即可。

#include <iostream>
using namespace std;
void solve(int n) {int r = n - 1; // 菱形的半径for (int y = -r; y <= r; ++y) {for (int x = -r; x == r; ++x) {if (abs(x) + abs(y) <= r) {cout << "*";} else {cout << " ";}}cout << endl;}
}
int main() {int n;cin >> n;solve(n);return 0;
}

3 一些场景

曼哈顿距离(Manhattan Distance)是计算二维平面中两点之间距离的一种方法,具体的定义如下:
d = ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ d = |x_1 - x_2| + |y_1 - y_2| d=x1x2+y1y2

路径规划与网格图问题
曼哈顿距离常用于网格图中的路径规划问题,特别是在不允许斜向移动的情况下。例如:在一个 n × m n \times m n×m 的网格图中,每个格子可能有障碍物或空地。从起点到终点的最短路径是多少?

  • 使用BFS结合曼哈顿距离作为启发式函数。
  • 曼哈顿距离可以估算当前点到目标点的最小步数,从而优化搜索效率。

棋盘问题
在一个 n × n n \times n n×n 的棋盘上,骑士从起点移动到终点所需的最少步数是多少?

  • 使用 BFS 搜索所有可能的移动路径。
  • 曼哈顿距离可以作为启发式函数,估算当前点到目标点的距离,从而优化搜索方向。

城市配送问题

给定一个 n × n n \times n n×n 的网格图,某些格子上有货物需求,另一些格子上有配送中心。每个配送中心只能服务一定范围内的需求点。求所有需求点到最近配送中心的总曼哈顿距离。

#include <iostream>
#include <queue>
#include <vector>
using namespace std;const int INF = 1e9;
const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int minTotalDeliveryDistance(int n, vector<pair<int, int>>& depots, vector<pair<int, int>>& demands) {vector<vector<int>> distance(n, vector<int>(n, INF));queue<pair<int, int>> q;for (auto& depot : depots) {int x = depot.first, y = depot.second;distance[x][y] = 0;q.push({x, y});}while (!q.empty()) {auto [x, y] = q.front();q.pop();for (int i = 0; i < 4; ++i) {int nx = x + dirs[i][0], ny = y + dirs[i][1];if (nx >= 0 && nx < n && ny >= 0 && ny < n && distance[nx][ny] > distance[x][y] + 1) {distance[nx][ny] = distance[x][y] + 1;q.push({nx, ny});}}}long long totalDistance = 0;for (auto& demand : demands) {int x = demand.first, y = demand.second;if (distance[x][y] == INF) return -1; // 如果无法到达totalDistance += distance[x][y];}return totalDistance;
}int main() {int n = 5;vector<pair<int, int>> depots = {{0, 0}, {4, 4}};vector<pair<int, int>> demands = {{2, 2}, {3, 3}};cout << "ret: " << minTotalDeliveryDistance(n, depots, demands) << endl;return 0;
}
http://www.dtcms.com/wzjs/90670.html

相关文章:

  • 上海福州路附近做网站的公司恢复2345网址导航
  • 响水县住房建设局网站宁波seo在线优化哪家好
  • 武清做网站的公司2345网址导航怎么卸载
  • 天津网络网站制作seo职业技能培训班
  • 有手机网站了还要微网站吗企业整站推广
  • 如何百度搜到网站百度域名收录
  • 网站建设哪家强市场营销案例150例
  • 校园网站开发的需求和分析建站网站
  • 男女做男个真实视频网站北京seo排名外包
  • 购物网站开发的基本介绍cpm广告联盟平台
  • 学做家常菜去那个网站广告推广的软件
  • 百度提交网站收录查询百度seo报价
  • 广告设计公司的简介优化师和运营区别
  • 微商怎么开通南宁网站优化
  • 做图骂人的图片网站潍坊seo推广
  • 信阳做网站 汉狮网络seo优化网络公司
  • 网站建设标志设计软文网站推荐
  • 有没有免费网站建设近期时事新闻10条
  • 短视频剪辑自学seo关键词优化哪个平台好
  • 武汉城乡建设部网站首页企业网络规划与设计
  • 用flash做的网站欣赏杯子软文营销300字
  • 手机在线建站广告公司注册
  • 站长之家是干嘛的百度优化插件
  • 做企业网站需要收费吗想做网站找什么公司
  • 做网站是什么专业什么工作南通企业网站制作
  • 专业代做简历网站最近一周的重大新闻
  • 西安网站建设外包西安危机公关公司
  • 重庆网上注册公司网站下载安装百度一下
  • 做教育网站宣传策略seo与sem的区别和联系
  • ASP做购物网站视频网站创建免费用户