POJ 1032 解题报告
2015-09-01 04:39
316 查看
P.S. 之前按照列表做,做着做着就做岔了。这里回归正轨。
这道题后来做成了找规律题(我用递归搜索不出意外地TLE了)。discuss 上面有好些关于规律的讨论和证明,比如:http://poj.org/showmessage?message_id=98514
知道后就简单了很多了。
这里参考了http://blog.csdn.net/synapse7/article/details/18237045的规律总结。
这道题后来做成了找规律题(我用递归搜索不出意外地TLE了)。discuss 上面有好些关于规律的讨论和证明,比如:http://poj.org/showmessage?message_id=98514
知道后就简单了很多了。
这里参考了http://blog.csdn.net/synapse7/article/details/18237045的规律总结。
thestoryofsnow | 1032 | Accepted | 144K | 0MS | C++ | 1028B |
/* ID: thestor1 LANG: C++ TASK: poj1032 */ #include <iostream> #include <fstream> #include <cmath> #include <cstdio> #include <cstring> #include <limits> #include <string> #include <vector> #include <list> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cassert> using namespace std; // see http://blog.csdn.net/synapse7/article/details/18237045 for explanation int main() { // 5 <= N <= 1000 int N; scanf("%d", &N); int i = 2, sum = 0; while (sum <= N) { sum += i; i++; } i--; sum -= i; i--; // printf("i: %d, sum: %d\n", i, sum); int left = N - sum; // 1.若剩余值(n-sum)等于i,则最后输出序列为:3,4,...,i,i+2,即将原最大序列每项加1,再将最后剩余的一个1加到最后一项上。 if (left == i) { for (int j = 3; j <= i; ++j) { printf("%d ", j); } printf("%d\n", i + 2); } else { assert (left < i); for (int j = 2; j <= 1 + i - 1 - left; ++j) { printf("%d ", j); } for (int j = 1 + i - 1 - left + 1; j <= i; ++j) { printf("%d ", j + 1); } printf("\n"); } return 0; }
相关文章推荐
- Visual Studio 2015的C++开发新特性(链接)
- 各种居中问题
- LeetCode Partition List
- LeetCode Partition List
- [LeetCode#263]Factorial Trailing Zeroes
- 做软件不要忘了做荷兰文
- Oracle+Ado.Net(四)
- 卷积神经网络小结(Convolutional Neural Networks)
- LeetCode Linked List Cycle II
- LeetCode Linked List Cycle II
- 深度卷积神经网络CNNs的多GPU并行框架及其应用
- cuDNN:利用 GPU 加速卷积神经网络
- Javascript 学习
- Cocos2d-x学习笔记之CCScene、CCLayer、CCSprite的默认坐标和默认锚点实验
- 梦想、执着
- Emacs使用(一)
- iGriamceV8.0 IG8.0.0 iOS8 V8基本设置和使用教程图
- JavaScript-DOM学习笔记之动态创建标记
- [LeetCode#204]Factorial Trailing Zeroes
- iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值