Bubble (opentrains)
来源:互联网 发布:周杰伦 斗牛 知乎 编辑:程序博客网 时间:2024/06/15 22:30
规律题
要求生成一个1-n的序列,冒泡排序后每个数字都只交换两次
通过一个dp发现只要数列能够分成3*x+4*y的形式就可以生成
3位的[1,2,3]变成[3,2,1]
4位的[1,2,3,4]变成[3,4,1,2]
按照这个规律就可以了
#include <bits/stdc++.h>using namespace std;const int maxn = 110;int f[maxn], n, a[maxn], l, r, x, y;int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d", &n); f[3] = 1; f[4] = 1; for (int i = 5; i <= n; i++) f[i] = f[i-3]+f[i-4]; printf("%d\n", f[n]); if (f[n] != 0) { x = 0; while (1) { if ((n-3*x)%4 == 0) { y = (n-3*x)/4; break; } x++; } l = 1; for (int i = 1; i <= x; i++) { r = l+2; a[l] = l+2; a[l+1] = l+1; a[l+2] = l; l = l+3; } for (int i = 1; i <= y; i++) { r = l+3; a[l] = l+2; a[l+1] = l+3; a[l+2] = l; a[l+3] = l+1; l = l+4; } for (int i = 1; i < n; i++) printf("%d ", a[i]); printf("%d\n", a[n]); }}
阅读全文
0 0
- Bubble (opentrains)
- Sequence (opentrains)
- Selection (opentrains)
- Maze (opentrains)
- Bubble
- bubble
- Tree packing opentrains
- Blobs' Exhibition opentrains
- Bubble sort
- bubble.c
- bubble.cpp
- Bubble Sort
- bubble sort
- Bubble Sort
- Bubble Sort
- bubble sort
- Bubble Sort
- bubble sorting
- andorid 进程通信机制
- “逐语句”、“逐过程”、“跳出”命令 (“调试”菜单)的使用
- 火狐不支持enter事件
- NumPy基础
- NFS服务安装
- Bubble (opentrains)
- eclipse中mybatis generator插件的安装及使用
- VB程序设计总结
- 破解MyEclipse
- 让安全责任成为一种习惯
- 深入理解git push
- 刷题——Roadblocks POJ
- Windows+SonarQube+Jenkins+Git+SonarPython配置持续集成的代码质量平台
- 分页查询简单实现(Freemarker+SpringMVC+Mybatis)