题意:给你两个数,一个n表示这个三角有多少层,一个sum表示总和

思路:

类似杨辉三角

1

1       1

1      2     1

第n行的第k个数 为 n!/k!(n-k)!

  1. 暴力枚举,因为杨辉三角每行的第一个数都是1,所以你需要每行都乘上一个系数
  2. 排列系数

解决问题的代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
int c(int n, int k)
{
int result = ;
for (int i = ; i < k; i++)
result = result * (n - i) / (i + );
return result;
}
int main()
{
int n, sum;
int line[];
scanf("%d%d", &n, &sum);
for (int i = ; i < ; i++)
line[i] = i + ;
do {
int result = ;
for (int i = ; i < n; i++)
result += c(n - , i)*line[i];
if (result == sum)
break;
} while (next_permutation(line, line + n));
for (int i = ; i < n; i++)
printf("%d ", line[i]);
printf("\n");
return ;
}

poj 3187 三角数问题的更多相关文章

  1. POJ 3187【permutation】

    POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include &l ...

  2. POJ 3187 Backward Digit Sums 枚举水~

    POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

  3. 穷竭搜索:POJ 3187 Backward Digit Sums

    题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...

  4. next_permutation暴力搜索,POJ(3187)

    题目链接:http://poj.org/problem?id=3187 解题报告: #include <stdio.h> #include <iostream> #includ ...

  5. Enum:Backward Digit Sums(POJ 3187)

    反过来推 题目大意:就是农夫和这只牛又杠上了(怎么老是牛啊,能换点花样吗),给出一行数(从1到N),按杨辉三角的形式叠加到最后,可以得到一个数,现在反过来问你,如果我给你这个数,你找出一开始的序列(可 ...

  6. POJ 3187 杨辉三角+枚举排列 好题

    如果给出一个由1~n组成的序列,我们可以每相邻2个数求和,得到一个新的序列,不断重复,最后得到一个数sum, 现在输入n,sum,要求输出一个这样的排列,如果有多种情况,输出字典序最小的那一个. 刚开 ...

  7. POJ 3187 穷举

    题意:已知有N个数分别为1-N,如下图为4个数.相邻两两相加直至只剩下一个数,下图的结果就是16. 3 1 2 4     4 3 6   7 9 16 现在反过来看,告诉你数的个数N和最终结果,问这 ...

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

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  9. POJ 3187 Backward Digit Sums

    暴力DFS+验证. 验证如果是暴力检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理. #include<cstdio> #include<cstring& ...

随机推荐

  1. (转)Linux基础知识学习

    Linux基础知识学习 原文:http://blog.csdn.net/ye_wei_yang/article/details/52777499 一.Linux的磁盘分区及目录 Linux的配置是通过 ...

  2. 持续集成~Jenkins构建dotnetCore的项目

    上周一个大件就是dotnet core2.0发布了,伴随着.NET Standard2.0也发布了,整个微软的生态环境大好,当然也有一个BUG出来了,比如EFCore对Mysql的支持比起1.1来说, ...

  3. 把一个HashMap的值全部取出来,放到两个数组中

    先是从数据库中获取所有的值,返回一个HashMap类型的数据: <pre name="code" class="java"> private Has ...

  4. LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 .NET 4.5 installed Visual Studio 2012 Release Preview

    Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt' after ...

  5. virtualapk爬坑心得

    1.宿主和插件的工程build.gradle必须是 com.android.tools.build:gradle:2.1.3 gradle-wrapper 必须是 gradle-2.14.1-all ...

  6. ios 开发发布证书配置详细流程

    iOS证书配置实践 本文参考了: iOS证书配置指南:http://dev.umeng.com/push/ios/license-configuration-guide 写在前面: 团队开发证书的管理 ...

  7. $'\r': command not found 或者 syntax error: unexpected end of file 或者 syntax error near unexpected token `$'\r''

    执行shell脚本如果报如下错误: syntax error near unexpected token `$'\r'' syntax error: unexpected end of file $' ...

  8. c/c++的const和static区别

    C语言中的const和static用来修饰变量或者函数,用const修饰表示不可改变,用static修饰表示变量或者函数是静态的,作用域控制在函数内. const定义的常量在超出其作用域之后其空间会被 ...

  9. userBean-作用范围session

    package com.java1234.model; public class Student { private String name;private int age; public Strin ...

  10. GPnP profile内容

    <?xml version="1.0" encoding="UTF-8"?>  <gpnp:GPnP-Profile Version=&quo ...