C++--打印*号

输入4
打印:* *
    *
   * *

#include <iostream>
using namespace std;

int main()
{
    int n;
    cout << "input n: ";
    cin >> n;
    for(int i = 1; i < n; i++)
    {
        for(int j = 1; j < n; j++)
        {

            if(j == i || j == n-i)
                cout << "*";
            else
                cout << " ";
            if(j >= i && j >= n-i)
            {
                cout << endl;
                break;
            }

        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Informed-RRT*算法是一种用于路径规划的算法,它可以在高维空间找到一条最短路径。下面是使用C语言实现Informed-RRT*算法的示例代码。 ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define WIDTH 500 // 定义环境宽度 #define HEIGHT 500 // 定义环境高度 typedef struct node{ // 定义树节点 int x, y; float cost; struct node *parent; } Node; typedef struct{ // 定义目标点 int x, y; } Goal; float euclidean_distance(Node *node1, Node *node2){ // 计算欧几里得距离 float dx = node1->x - node2->x; float dy = node1->y - node2->y; return sqrt(dx*dx + dy*dy); } Node *generate_random_node(){ // 随机生成节点 Node *node = (Node *)malloc(sizeof(Node)); node->x = rand() % WIDTH; node->y = rand() % HEIGHT; node->cost = 0.0; node->parent = NULL; return node; } Node *nearest_vertex(Node *node, Node **tree, int tree_size){ // 找到最近的节点 float min_distance = 999999.0; Node *nearest_node = NULL; for(int i=0; i<tree_size; i++){ float distance = euclidean_distance(node, tree[i]); if(distance < min_distance){ min_distance = distance; nearest_node = tree[i]; } } return nearest_node; } Node *new_node(Node *nearest_node, Node *random_node, float step_size){ // 创建新节点 Node *new_node = (Node *)malloc(sizeof(Node)); float distance = euclidean_distance(nearest_node, random_node); if(distance < step_size){ // 如果距离小于步长,直接连接两个节点 new_node->x = random_node->x; new_node->y = random_node->y; } else{ // 否则在两个节点之间插值 float theta = atan2(random_node->y - nearest_node->y, random_node->x - nearest_node->x); new_node->x = nearest_node->x + step_size * cos(theta); new_node->y = nearest_node->y + step_size * sin(theta); } new_node->cost = nearest_node->cost + euclidean_distance(nearest_node, new_node); new_node->parent = nearest_node; return new_node; } void add_node_to_tree(Node *node, Node **tree, int *tree_size){ // 将节点添加到树 tree[*tree_size] = node; (*tree_size)++; } Goal *generate_random_goal(){ // 随机生成目标点 Goal *goal = (Goal *)malloc(sizeof(Goal)); goal->x = rand() % WIDTH; goal->y = rand() % HEIGHT; return goal; } Node *find_best_goal_node(Node **tree, int tree_size, Goal *goal){ // 找到最优的目标节点 float min_distance = 999999.0; Node *best_node = NULL; for(int i=0; i<tree_size; i++){ float distance = euclidean_distance(tree[i], (Node *)goal); if(distance < min_distance){ min_distance = distance; best_node = tree[i]; } } return best_node; } void rewire(Node *node, Node **tree, int tree_size, float step_size){ // 重新连接节点 for(int i=0; i<tree_size; i++){ Node *near_node = tree[i]; float distance = euclidean_distance(node, near_node); float new_cost = node->cost + distance; if(new_cost < near_node->cost){ // 如果新的路径比原来的路径更短,则重新连接节点 float theta = atan2(node->y - near_node->y, node->x - near_node->x); int new_x = near_node->x + step_size * cos(theta); int new_y = near_node->y + step_size * sin(theta); if(new_x > 0 && new_x < WIDTH && new_y > 0 && new_y < HEIGHT){ // 确保新节点在环境内 near_node->parent = node; near_node->cost = new_cost; } } } } void print_path(Node *node){ // 打印路径 while(node != NULL){ printf("(%d,%d) ", node->x, node->y); node = node->parent; } printf("\n"); } void informed_rrt_star(int max_iterations, float step_size){ srand((unsigned int)time(NULL)); // 设置随机种子 Node *start_node = generate_random_node(); // 随机生成起始节点 Node *tree[10000]; int tree_size = 1; tree[0] = start_node; Goal *goal = generate_random_goal(); // 随机生成目标点 for(int i=0; i<max_iterations; i++){ // 执行迭代 Node *random_node = generate_random_node(); // 随机生成节点 Node *nearest_node = nearest_vertex(random_node, tree, tree_size); // 找到最近的节点 Node *new_node = new_node(nearest_node, random_node, step_size); // 创建新节点 if(new_node->x == nearest_node->x && new_node->y == nearest_node->y){ // 如果新节点和最近节点重合,则跳过 continue; } if(new_node->x == goal->x && new_node->y == goal->y){ // 如果新节点是目标节点,则返回路径 print_path(new_node); return; } if(new_node->x < 0 || new_node->x > WIDTH || new_node->y < 0 || new_node->y > HEIGHT){ // 如果新节点不在环境内,则跳过 continue; } add_node_to_tree(new_node, tree, &tree_size); // 将新节点添加到树 rewire(new_node, tree, tree_size, step_size); // 重新连接节点 } Node *best_goal_node = find_best_goal_node(tree, tree_size, goal); // 找到最优的目标节点 print_path(best_goal_node); // 打印路径 } int main(){ informed_rrt_star(1000, 10.0); return 0; } ``` 上述代码使用了一个简单的环境,节点和目标点都是二维坐标。算法首先随机生成一个起始节点和目标点,然后在环境随机生成节点,并将其与树的最近节点连接。如果新节点和最近节点重合,则跳过。如果新节点是目标节点,则返回路径。如果新节点不在环境内,则跳过。然后将新节点添加到树,并重新连接节点。最后找到最优的目标节点,并返回路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值