微信搜索:编程笔记本。获取更多干货!
微信搜索:编程笔记本。获取更多干货!
点击上方蓝字关注我,我们一起学编程
欢迎小伙伴们分享、转载、私信、赞赏
1 火星文表示法
题目描述:
微信搜索:编程笔记本。获取更多干货!
微信搜索:编程笔记本。获取更多干货!
火星使用三进制表示数据,他们的数字符号:@
、$
、&
这三个符号分别表示 0、1、2 。现在输入一个十进制数,请把它用火星的三进制数表示出来。
示例:
输入:123
输出:"$$$&@"
参考代码:
string triCoding(int num){vector<int> code;while (num != 0) {code.push_back(num % 3);num /= 3;}string s;string base = "@$&";for (auto e : code) {s += base[e];}return s;}
2 找出离质心最近的点
微信搜索:编程笔记本。获取更多干货!
微信搜索:编程笔记本。获取更多干货!
题目描述:
在二维平面上有 k(0~k-1) 个点,请找出这 k 个点中离质心最近点的编号。质心:横纵坐标为所有点的均值。
示例:
输入:["1,1", "2,2", "1,2", "1,3"]
输出:2
参考代码:
int get_index(vector<string> points){double x, y;double mx = 0, my = 0;vector<double> pos;// 从string中分析横纵坐标for (sting s : points) {x = 0;y = 0;int idx = 0;while (s[idx] != ',') {x = x * 10 + s[idx++] - '0';}pos.push_back(x);mx += x;++idx;while (idx < s.size()) {y = y * 10 + s[idx++] - '0';}pos.push_back(y);my += y;}mx /= points.size();my /= points.size();int index = 0;double min_dist = INT_MAX;for (int i = 0; i < pos.size(); i += 2) {x = pos[i];y = pos[i + 1];double dist = (x - mx) * (x - mx) + (y - my) * (y - my);if (dist < min_dist) {min_dist = dist;index = i / 2;}}return index;}
3 扑克牌排序
微信搜索:编程笔记本。获取更多干货!
微信搜索:编程笔记本。获取更多干货!
题目描述:
一副扑克牌中的牌面分为 5 种花色:大小鬼、黑桃、红桃、梅花、方块,分别用k
、s
、h
、p
、q
表示这五种花色,同时牌面采用1~13
之间的数字表示同一花色中的大小(小鬼-1,大鬼-2)。某扑克牌游戏中,一个玩家拿到了 12 张扑克牌,请按照如下规则对该玩家的牌进行排序,返回排序结果。
首先按照大小鬼、、黑桃、红桃、梅花、方块进行花色排序;然后再对同一花色中的扑克牌按照数字从小到大排序。
示例:
输入:["s1", "s3", "s9", "s4", "h1", "p3", "p2", "q5", "q4", "q9", "k2", "k1"]
输出:["k1", "k2", "s1", "s3", "s4", "s9", "h1", "p2", "p3", "q4", "q5", "q9"]
参考代码:
vector<string> getPokerOrder(vector<string> cards){vector<string> res;string help = "kshpq";map<char, vector<string>> type;for (int i = 0; i < cards.size(); ++i) {type[cards[i][0]].push_back(cards[i]);}for (int i = 0; i < help.size(); ++i) {sort(type[help[i]].begin(), type[help[i]].end());for (auto e : type[help[i]]) {res.push_back(e);}}return res;}
微信搜索:编程笔记本。获取更多干货!
微信搜索:编程笔记本。获取更多干货!