USACO2006 Backward Digit Sums /// 全排列 oj24212
题目大意:
给出杨辉三角的顶点值M和底边数的个数 N (1 ≤ N ≤ 10) ,求出底边各个数的值,其中各个数范围都为1 ~ N
当N=4,M=16时可能是这样的
3 1 2 4
4 3 6
7 9
16
Multiple test cases. For each case:
* Line 1: Two space-separated integers: N and the final sum.
For each case, output one line : 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.
4 16
3 1 2 4
Explanation of the sample:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
3 1 2 4不是唯一解, 但是按字典序的最小解
N较小 直接暴力 用next_permutation()全排列函数 得出下一种排列 验证正确则跳出结束
#include <bits/stdc++.h>
using namespace std;
int a[],b[],n,m;
bool judge()
{
for(int k=n;k>;k--)
{
for(int i=;i<k;i++)
b[i]=b[i]+b[i+];
}
if(b[]==m) return ;
else return ;
}
void per()
{
do
{
for(int i=;i<=n;i++)
b[i]=a[i];
if(judge()) return;
}while(next_permutation(a+,a++n));
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
a[i]=i;
per();
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
} }
USACO2006 Backward Digit Sums /// 全排列 oj24212的更多相关文章
- BZOJ1653: [Usaco2006 Feb]Backward Digit Sums
1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 207 Solved: ...
- 1653: [Usaco2006 Feb]Backward Digit Sums
1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 285 Solved: ...
- Backward Digit Sums(暴力)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5664 Accepted: 32 ...
- 【POJ - 3187】Backward Digit Sums(搜索)
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...
- P1118 [USACO06FEB]Backward Digit Sums G/S
P1118 [USACO06FEB]Backward Digit Sums G/S 题解: (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N. 对每个 ...
- Backward Digit Sums(POJ 3187)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5495 Accepted: 31 ...
- POJ3187 Backward Digit Sums 【暴搜】
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4487 Accepted: 25 ...
- POJ 3187 Backward Digit Sums 枚举水~
POJ 3187 Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3 1 2 4 他可以相邻 ...
- (DFS、全排列)POJ-3187 Backward Digit Sums
题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难 ...
随机推荐
- shell 检查文件夹是否包含文件,或者只是空文件
empty_dir_check(){ check_dir=$ if [ -d $check_dir ];then file_list=` -maxdepth -type f` if [ $file_l ...
- Git 学习第一天
本文是根据廖雪峰老师的git教程记录的学习笔记,特此说明,原教程链接https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c ...
- 20140814 explicit
1.explicit explicit 只对构造函数起作用,用来抑制隐式转换. 如: class A { A(int a); }; int Functi ...
- Feign 系列(02)Why Feign
Feign 系列(02)Why Feign [toc] 1. 什么是 Feign Feign 的英文表意为"假装,伪装,变形", 是一个 Http 请求调用的轻量级框架,可以以 J ...
- 连接分析算法-HITS-算法
转自http://blog.csdn.net/Androidlushangderen/article/details/43311943 参考资料:http://blog.csdn.net/hguisu ...
- STL 队列模板实现
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010016150/article/details/32715801 C++ Prime确实有点 ...
- SingalR 构建 推送服务器初探
项目需要用到推送,于是重新研究了下推送框架,最好能够独立成一个服务,与业务无关的服务,可以给所有的项目通用.找了好久最终决定用SinglR 框架. Signal 是微软支持的一个运行在 Dot NET ...
- mac 如何卸载node和npm采坑之旅
因为本地npm一直报错,所以决定直接卸载node和npm,重新装.第一次卸载,具体咱也不会呀!能咋整呢,百度呗 茫茫百度中各种找呀,找到一个转载最多的方法 sudo npm uninstall npm ...
- 2019-4-29-.NET-Standard
title author date CreateTime categories .NET Standard lindexi 2019-4-29 12:7:26 +0800 2018-2-13 17:2 ...
- 深度学习攻防对抗(JCAI-19 阿里巴巴人工智能对抗算法竞赛)
最近在参加IJCAI-19阿里巴巴人工智能对抗算法竞赛(点击了解),初赛刚刚结束,防御第23名,目标攻击和无目标攻击出了点小问题,成绩不太好都是50多名,由于找不到队友,只好一个人跟一群大佬PK,双拳 ...