一个长度13的尺子,如果在1位置刻点可以量出1和12,13三种刻度.那么至少刻几个点,可以直接量出1-13所有的长度,分别刻在哪几个位置?

注:必须是直接量。即在尺子上能找出一个1-13任意的整数长度。

写了个没什么技术含量的dfs暴力求解。一个可行解是 1, 2, 6, 10。

 #include <iostream>
#include <vector>
#include <unordered_map>
using namespace std; class Solution {
public:
vector<vector<int>> ruler(int n, vector<int> &minPath) {
dfs(, n, minPath);
return result;
}
vector<vector<int>> result;
vector<int> path;
void dfs(int start, int n, vector<int> &minPath) {
if (start == n + ) {
if (fullScale(path)) {
result.push_back(path);
if (path.size() < minLen) {
minLen = path.size();
minPath = path;
}
}
return;
}
for (int i = start; i <= n; i++) {
path.push_back(i);
dfs(i + , n, minPath);
path.pop_back();
}
}
bool fullScale(vector<int> path) {
if (path.size() < ) {
return false;
}
unordered_map<int, int> umap;
umap[]++;
umap[]++;
for (int i = ; i < path.size(); i++) {
for (int j = ; j < i; j++) {
if (path[i] - path[j] < ) {
umap[path[i] - path[j]]++;
umap[path[j]]++;
umap[path[i]]++;
umap[ - path[i]]++;
umap[ - path[j]]++;
}
if (umap.size() >= ) {
return true;
}
}
}
return false;
}
private:
int minLen = ;
}; int main() {
int n = ;
Solution solu;
vector<int> minPath;
vector<vector<int>> res = solu.ruler(n, minPath);
for (auto x : minPath) {
cout << x << ", ";
}
}

ref: https://en.wikipedia.org/wiki/Sparse_ruler

最新文章

  1. HttpClient 4.5.x 工具类设计与实现
  2. 一个通过网络转换Ico到Png图片的小小程序(Ico2Png)
  3. 《30天自制操作系统》14_day_学习笔记
  4. python多进程共享变量Value使用tips
  5. fetion for linux
  6. 平衡搜索树(二) Rb 红黑树
  7. 字符串编码---hash函数的应用
  8. Android项目中gen文件下R文件无法生成的解决的方法
  9. 介绍一个成功的 Git 分支模型 Release 分支
  10. 请教下关于CKEditor富文本编辑框设置字体颜色的问题
  11. Java字符编码
  12. django csrftoken
  13. Vue框架是什么,有什么特点,怎么用
  14. linux 杀死进程kill 等用法
  15. poj2965 【枚举】
  16. vs 2015 编译cocos2d-x-3.9
  17. cmd命令关闭占用程序的端口
  18. UVA-10539 Almost Prime Numbers
  19. MySQL学习笔记:Engine存储引擎
  20. OA之框架的搭建

热门文章

  1. MD5 工具类
  2. Jmeter创建一个 JMS 主题的测试计划
  3. css3记事
  4. Linux 进程以及多线程的支持
  5. python-queue队列通信
  6. &quot;text&quot;和new String(&quot;text&quot;)的区别
  7. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)
  8. R语言列表list函数
  9. Android6.0内核移植(2):kernel编译内核
  10. JavaScript的柯里化函数