POJ 3187 Backward Digit Sums 题解 《挑战程序设计竞赛》

题目:POJ 3187

思路:

这道题很简单,用next_permutation枚举1~N的所有排列,然后依次相加,判断最后的和是否等于sum,是的话则break,即为字典序最前的。

 1 #include <iostream>
 2 #include <algorithm>
 3
 4 using namespace std;
 5
 6 int n;
 7 int sum;
 8 int mycase[11][11];
 9
10 int main() {
11     cin >> n >> sum;
12     for (int i = 1; i <= n; i++) {
13         mycase[n][i] = i;
14     }
15     do {
16         for (int i = n; i > 1; i--) {
17             for (int j = 1; j < i; j++) {
18                 mycase[i - 1][j] = mycase[i][j] + mycase[i][j + 1];
19             }
20         }
21         if (mycase[1][1] == sum) {
22             for (int i = 1; i <= n; i++) {
23                 cout << mycase[n][i] << " ";
24             }
25             break;
26         }
27     } while (next_permutation(mycase[n] + 1, mycase[n] + 1 + n));
28     return 0;
29 }

注意:

  • next_permutaiton()的两个参数分别是枚举起始的元素,和结束的元素的后一个元素。
时间: 2024-10-21 05:11:02

POJ 3187 Backward Digit Sums 题解 《挑战程序设计竞赛》的相关文章

POJ 3187 Backward Digit Sums(next_permutation)

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

POJ 3187 Backward Digit Sums

http://poj.org/problem?id=3187 穷竭搜索 全排列 然后按规则求和 排列之前先按升序排序 这样可以保证第一个和为k的就是符合最小序列的结果 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int sum(int a[], int n) 8 { 9 int s[11]; 10 for (int i = 0

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

poj 3187 Backward Digit Sums(穷竭搜索dfs)

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

POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)

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. Fo

POJ 题目Backward Digit Sums(next_permutation)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4807   Accepted: 2772 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 ad

【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题

学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二分图最大匹配即可. 邻接矩阵,DFS寻找增广路,匈牙利算法 邻接矩阵:复杂度O(n^3) 如果使用邻接表:复杂度O(n*m) #include<cstdio> #include<cstring> #include<cmath> #include<iostream>

【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题

不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dinic,复杂度:O(V2E) 直接套用模板 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set

【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

[题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个汇点,源点到结点1连一条最大流量为2,费用为0的边,结点N到汇点连一条最大流量为2,费用为0的边,这样就规定好流量了. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #