poj 3187 Backward Digit Sums 【STL暴力】
2015-12-20 13:35
281 查看
Backward Digit Sums
Description
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example,
one instance of the game (when N=4) might go like this:
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Input
Line 1: Two space-separated integers: N and the final sum.
Output
Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.
Sample Input
Sample Output
Hint
Explanation of the sample:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
题意:给定一个数N,表示杨辉三角的层数。现在让你找到一个字典序最小的1-N的排列使得以该排列为第一层的杨辉三角的最后一层是m。
思路:N最多为10,用next_permutation比较方便吧。
AC代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5413 | Accepted: 3123 |
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example,
one instance of the game (when N=4) might go like this:
3 1 2 4 4 3 6 7 9 16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Input
Line 1: Two space-separated integers: N and the final sum.
Output
Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.
Sample Input
4 16
Sample Output
3 1 2 4
Hint
Explanation of the sample:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
题意:给定一个数N,表示杨辉三角的层数。现在让你找到一个字典序最小的1-N的排列使得以该排列为第一层的杨辉三角的最后一层是m。
思路:N最多为10,用next_permutation比较方便吧。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define INF 0x3f3f3f #define eps 1e-8 #define MAXN (1000000+1) #define MAXM (100000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%.2lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; int Map[20][20]; int num[20]; bool judge(int n, int sum) { for(int i = 1; i <= n; i++) Map [i] = num[i]; for(int i = n-1; i >= 1; i--) for(int j = 1; j <= i; j++) Map[i][j] = Map[i+1][j] + Map[i+1][j+1]; return Map[1][1] == sum; } int main() { int N, M; while(scanf("%d%d", &N, &M) != EOF) { for(int i = 1; i <= N; i++) num[i] = i; do { if(judge(N, M)) { for(int i = 1; i <= N; i++) { if(i > 1) printf(" "); printf("%d", num[i]); } printf("\n"); break; } } while(next_permutation(num+1, num+N+1)); } return 0; }
相关文章推荐
- jqurey简介
- 统计题1
- 画了一张PHPCMSV9的运行流程思维导图
- SQL Server中全角半角的转换
- hdoj 3552 I can do it! 【思维】
- 三目运算符潜规则
- 逆向工程
- 几个编程的基本原则,基础但是不能忘记!
- SpringMVC异常体系
- [LeetCode] 306. Additive Number [Medium]
- DES加密之3DES
- 1005. F.Snowy Roads最小生成树Kruskal算法
- binbinyang博客-Android 百度地图 ---覆盖物 maker
- 孙悟空的坚定目标 (孙悟空有何深仇大恨大闹天宫)
- Java在MySQL数据库中删除、更新、循环插入的例子
- 【Linux运维入门】JMX方式远程监控Linux下JVM运行情况
- VB.NET Declare语句
- ListView弹性下拉效果
- Nginx
- Maven pom.xml 文件报错