当前位置: 首页 > news >正文

每日一题 第二十一期 洛谷 组合的输出

组合的输出

题目描述

排列与组合是常用的数学方法,其中组合就是从 n n n 个元素中抽出 r r r 个元素(不分顺序且 r ≤ n r \le n rn),我们可以简单地将 n n n 个元素理解为自然数 1 , 2 , … , n 1,2,\dots,n 1,2,,n,从中任取 r r r 个数。

现要求你输出所有组合。

例如 n = 5 , r = 3 n=5,r=3 n=5,r=3,所有组合为:

123 , 124 , 125 , 134 , 135 , 145 , 234 , 235 , 245 , 345 123,124,125,134,135,145,234,235,245,345 123,124,125,134,135,145,234,235,245,345

输入格式

一行两个自然数 n , r ( 1 < n < 21 , 0 ≤ r ≤ n ) n,r(1<n<21,0 \le r \le n) n,r(1<n<21,0rn)

输出格式

所有的组合,每一个组合占一行且其中的元素按由小到大的顺序排列,每个元素占三个字符的位置,所有的组合也按字典顺序。

注意哦!输出时,每个数字需要 3 3 3 个场宽。以 C++ 为例,你可以使用下列代码:

cout << setw(3) << x;

输出占 3 3 3 个场宽的数 x x x。注意你需要头文件 iomanip

样例 #1

样例输入 #1

5 3

样例输出 #1

1  2  31  2  41  2  51  3  41  3  51  4  52  3  42  3  52  4  53  4  5

AC代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;int n ,r;
int st[35];
int a[N];
int l;
void dfs(int step)
{if(step == r + 1){for(int i = 1; i <= r; i ++){cout << setw(3) << a[i];}cout << endl;return ;}for(int i = a[step - 1] + 1; i <= n; i ++){a[step] = i;dfs(step + 1);}return ;
}
int main()
{cin >> n >> r;
//	for(int i = 1; i <= r; i ++)
//	{
//		memset(a, 0, sizeof a);
//		l = i;
//		dfs(i);		
//	}dfs(1);return 0;
}
http://www.lryc.cn/news/322546.html

相关文章:

  • JavaScript 面试题
  • java输入语句scanner
  • Python从入门到精通秘籍十一
  • WRF模型教程(ububtu系统)-WPS(WRF Pre-Processing System)概述
  • C语言向C++过渡的基础知识(一)
  • GEE遥感云大数据林业应用典型案例及GPT模型应用
  • macOS Ventura 13.6.5 (22G621) Boot ISO 原版可引导镜像下载
  • 数据结构面试常见问题之Insert or Merge
  • perl 用 XML::LibXML 解析 Freeplane.mm文件,XML文件
  • Spring Cloud Alibaba微服务从入门到进阶(七)(服务容错-Sentinel)
  • Arduino RP2040 + SSD1306 I2C OLED +LittleFS存储GBK字库实现中文显示
  • 代码随想录算法训练营第day53|1143.最长公共子序列 、 1035.不相交的线、 53. 最大子序和 动态规划
  • 【Flutter学习笔记】10.2 组合现有组件
  • C++的vector类(一):vector类的常见操作
  • SpringBoot注解
  • 每日三个JAVA经典面试题(十九)
  • springboot企业级抽奖项目业务一(登录模块)
  • 【Python + Django】启动简单的文本页面
  • Docker——问题解决:服务器端和Windows端IP互通
  • HTTP跨域
  • 用Python的turtle库绘制皮卡丘
  • C语言打印当前时间
  • (一)基于IDEA的JAVA基础4
  • 【Python】复习12:标准库与第三方库
  • CUDA 12介绍
  • 旅游系统-软件与环境
  • AI基础知识(2)--决策树,神经网络
  • 蓝桥杯C++大学B组一个月冲刺记录2024/3/21
  • 由浅到深认识C语言(14):枚举
  • 速盾cdn:cdn节点缓存内容不一致怎么办?