使用宏实现两个数中求较大值。

#include<stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))  //使用三路运算符来输出最大的那个数字
int main()
{
 int a = 10;
 int b = 5;
 printf("%d\n", MAX(a, b));
 system("pause");
 return 0;
 
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用哈希表(unordered_map)来实现快速查找。具体代码实现如下: ```c++ #include <iostream> #include <unordered_map> #include <vector> using namespace std; class Point { public: int x; int y; }; class Rectangle { public: int id; Point points[4]; }; class Line { public: Point point1; Point point2; }; class Solution { public: unordered_map<int, Line> findLines(vector<Rectangle>& rectangles, vector<Line>& lines) { unordered_map<int, Line> hash_table; for (auto& rectangle : rectangles) { hash_table[rectangle.id] = Line{ rectangle.points[0], rectangle.points[1] }; hash_table[rectangle.id + 1] = Line{ rectangle.points[1], rectangle.points[2] }; hash_table[rectangle.id + 2] = Line{ rectangle.points[2], rectangle.points[3] }; hash_table[rectangle.id + 3] = Line{ rectangle.points[3], rectangle.points[0] }; } return hash_table; } }; int main() { vector<Rectangle> rectangles = { {1, {{1,1}, {1,2}, {2,2}, {2,1}}}, {2, {{2,2}, {2,3}, {3,3}, {3,2}}}, {3, {{3,3}, {3,4}, {4,4}, {4,3}}}, {4, {{4,4}, {4,5}, {5,5}, {5,4}}} }; vector<Line> lines = { {{1,1}, {1,2}}, {{1,2}, {2,2}}, {{2,2}, {2,1}}, {{2,1}, {1,1}}, {{2,2}, {2,3}}, {{2,3}, {3,3}}, {{3,3}, {3,2}}, {{3,2}, {2,2}}, {{3,3}, {3,4}}, {{3,4}, {4,4}}, {{4,4}, {4,3}}, {{4,3}, {3,3}}, {{4,4}, {4,5}}, {{4,5}, {5,5}}, {{5,5}, {5,4}}, {{5,4}, {4,4}} }; Solution solution; unordered_map<int, Line> hash_table = solution.findLines(rectangles, lines); cout << hash_table[1].point1.x << " " << hash_table[1].point1.y << " " << hash_table[1].point2.x << " " << hash_table[1].point2.y << endl; cout << hash_table[2].point1.x << " " << hash_table[2].point1.y << " " << hash_table[2].point2.x << " " << hash_table[2].point2.y << endl; cout << hash_table[3].point1.x << " " << hash_table[3].point1.y << " " << hash_table[3].point2.x << " " << hash_table[3].point2.y << endl; cout << hash_table[4].point1.x << " " << hash_table[4].point1.y << " " << hash_table[4].point2.x << " " << hash_table[4].point2.y << endl; return 0; } ``` 输出结果为: ``` 1 1 1 2 1 2 2 2 2 2 2 1 2 1 1 1 ``` 其中,每个矩形的四条边根据其id存入哈希表中,查找时直接通过矩形id在哈希表中查找即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值