题目描述

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

例如n=5,r=3,所有组合为:123,124,125,134,135,145,234,235,245,345。

输入格式

一行两个自然数n,r(1

输出格式

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

输入样例

5 3

输出样例

1  2  3

1  2  4

1  2  5

1  3  4

1  3  5

1  4  5

2  3  4

2  3  5

2  4  5

3  4  5(1)编程思路。

用递归来完成。

设函数void dfs(int pos,int num)表示为第pos(0≤pos≤r-1)个数取值,取值可以为num~n之一。显然,若r-pos>n-num+1,则后面剩下的数不够,直接剪枝;否则,在num~n中取一个数i(num≤i≤n)赋给a[pos],继续为下一个位置pos+1取数,即递归调用函数dfs(pos+1,i+1)。(2)源程序。

#include

int a[21],n,r;

void dfs(int pos,int num)

{

if (pos==r)   // 已有r个数

{

for (int i=0;i

printf("%3d",a);

printf("\n");

return;

}

if(r-pos>n-num+1) return ;

for(int i=num;i<=n;i++)

{

a[pos]=i;

dfs(pos+1,i+1);

}

}

int main()

{

scanf("%d%d",&n,&r);

dfs(0,1);

return 0;

}习题3232-1  Lotto

Description

In the German Lotto you have to select 6 numbers from the set {1,2,...,49}. A popular strategy to play Lotto - although it doesn't increase your chance of winning - is to select a subset S containing k (k > 6) of these 49 numbers, and then play several games with choosing numbers only from S. For example, for k=8 and S = {1,2,3,5,8,13,21,34} there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], ... [3,5,8,13,21,34].

Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.

Input

The input will contain one or more test cases. Each test case consists of one line containing several integers separated from each other by spaces. The first integer on the line will be the number k (6 < k < 13). Then k integers, specifying the set S, will follow in ascending order. Input will be terminated by a value of zero (0) for k.

Output

For each test case, print all possible games, each game on one line. The numbers of each game have to be sorted in ascending order and separated from each other by exactly one space. The games themselves have to be sorted lexicographically, that means sorted by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below. The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.

Sample Input

7 1 2 3 4 5 6 7

8 1 2 3 5 8 13 21 34

0

Sample Output

1 2 3 4 5 6

1 2 3 4 5 7

1 2 3 4 6 7

1 2 3 5 6 7

1 2 4 5 6 7

1 3 4 5 6 7

2 3 4 5 6 7

1 2 3 5 8 13

1 2 3 5 8 21

1 2 3 5 8 34

1 2 3 5 13 21

1 2 3 5 13 34

1 2 3 5 21 34

1 2 3 8 13 21

1 2 3 8 13 34

1 2 3 8 21 34

1 2 3 13 21 34

1 2 5 8 13 21

1 2 5 8 13 34

1 2 5 8 21 34

1 2 5 13 21 34

1 2 8 13 21 34

1 3 5 8 13 21

1 3 5 8 13 34

1 3 5 8 21 34

1 3 5 13 21 34

1 3 8 13 21 34

1 5 8 13 21 34

2 3 5 8 13 21

2 3 5 8 13 34

2 3 5 8 21 34

2 3 5 13 21 34

2 3 8 13 21 34

2 5 8 13 21 34

3 5 8 13 21 34

(1)编程思路。

本题的意思是要在有k个元素的集合S中任意取6个元素,并输出所有这些取值组合。

设 combine(int take[], int len, int count,int num[])为从具有len个元素的数组num中取出count个元素,并将取出的元素存放在数组a中。

为求解combine(int take[], int len, int count,int num[]),可以先在len个元素的数组num的后面取第一个元素num放在a[count-1]中,所取的第一个数组元素的下标i可以是len-1,len-2,…,count-1。注意:第一个取的数组元素的下标i不能取count-2,因为后面的要取的元素均会在第一个取的元素的前面,因此最多只能取出0~count-3共count-2个不同的y元素,达不到取count个数的目的。

在将确定组合的第一个元素num放入数组take后,有两种选择:还未确定组合的其余元素时(count>1,即还需取count-1个元素),继续递归comb(take,i,count-1,num)确定组合的其余元素,即在num[0]~num[i-1]这i个元素中取count-1个数;已确定组合的全部元素时(count==1),输出这个组合。

(2)源程序。

#include

void combine(int take[], int len, int count,int num[])

{

int i,j;

for (i = len-1; i >= count-1; i--)

{

take[count - 1] = num;

if (count >1)

combine(take, i , count - 1, num);

else

{

for (j = 6-1; j >=0; j--)

{

printf("%d ",take[j]);

}

printf("\n");

}

}

}

int main()

{

int i,k,num[13],take[6],t;

while(scanf("%d",&k) && k!= 0)

{

for(i = 0; i < k; i++)

scanf("%d",&num);

for (i=0;i

{

t=num;

num=num[k-i-1];

num[k-i-1]=t;

}

combine(take,k,6,num);

printf("\n");

}

return 0;

}32-2  选数

题目描述

已知 n 个整数x1 ,x2 ,…,xn,以及1个整数k(k

3+7+12=22

3+7+19=29

7+12+19=38

3+12+19=34。

现在,要求你计算出和为素数共有多少种。

例如上例,只有一种的和为素数:3+7+19=29。

输入格式

键盘输入,格式为:

n,kn,k(1≤n≤20,k

x1 ,x2 ,…,xn (1≤xi ≤5000000)

输出格式

1个整数(满足条件的种数)。

输入样例

4 3

3 7 12 19

输出样例

1

(1)编程思路。

编写递归函数void dfs(int pos,int num)完成n个数中取k个数。然后判断每种取值组合的和是否为一个素数。

(2)源程序。

#include

#include

int a[21],b[22],n,k,cnt=0;

int isPrime(int num)

{

if (num==1) return 0;

for (int m=2;m<=(int)sqrt((double)num);m++)

if (num%m==0) return 0;

return 1;

}

void dfs(int pos,int num)

{

if (pos==k)   // 已有k个数

{

int sum=0;

for (int i=0;i

sum+=a;

if (isPrime(sum)) cnt++;

return;

}

if(k-pos>n-num+1) return ;

for(int i=num;i<=n;i++)

{

a[pos]=b;

dfs(pos+1,i+1);

}

}

int main()

{

scanf("%d%d",&n,&k);

for (int i=1;i<=n;i++)

scanf("%d",&b);

dfs(0,1);

printf("%d\n",cnt);

return 0;

}32-3  加法变乘法

问题描述

我们都知道:1+2+3+ ... + 49 = 1225

现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015

比如:

1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015

就是符合要求的一个答案。

输入格式

输入一个正整数N (N>1225)。

输出格式

若可以将两个不相邻的加号变成乘号使得结果为N,输出两个乘号左边的数字,若解有多种情况,按字典序输出。如果无解,输出“No solution!”。

输入样例

2015

输出样例

10 27

16 24

(1)编程思路。

问题实际上是在1~48这个48个数字中任选两个数字。选择了这两个数字后,判断将两个数字后面的加号变成乘号后,表达式的值是否等于给定的N。

(2)源程序。

#include

int a[3],n,found=0;

void dfs(int pos,int num)

{

if (pos==2)

{

int t=1225-2*(a[0]+a[1]+1)+a[0]*(a[0]+1)+a[1]*(a[1]+1);

if (a[1]-a[0]!=1 && t==n)

{

printf("%d %d\n",a[0],a[1]);

found=1;

}

return;

}

if(2-pos>48-num+1) return ;

for(int i=num;i<=48;i++)

{

a[pos]=i;

dfs(pos+1,i+1);

}

}

int main()

{

scanf("%d",&n);

dfs(0,1);

if (found==0) printf("No solution!\n");

return 0;

}

c语言程序设计黑马答案,【上海校区】C语言程序设计100例之相关推荐

  1. 兰州大学c语言课程作业答案,2016兰州大学C语言程序设计课程作业1附答案.doc

    C语言程序设计课程作业_A 历次成绩完成时间查看详情1.0.02015-11-21 09:15:312.0.02015-11-21 09:15:243.0.02015-11-21 09:15:184. ...

  2. 2019上海理工大学c语言答案,上海理工大学C语言实验7答案.doc

    上海理工大学C语言实验7答案.doc (10页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 7.9 积分 实验7 字符数组和字符串参考答案[DIY]1. ...

  3. 郑州大学c语言期末练习答案,郑州大学《C语言程序设计》网上考试复习题2

    郑州大学<C语言程序设计>网上考试复习题2 本卷共有 3大题.一.单项选择题(35 道小题,共 70分)1.已知:char i='A':float f=11.5:正确的语句是():(2 分 ...

  4. 武汉理工大学c语言pta选择题答案,武汉理工大学c语言实验及答案.doc

    武汉理工大学c语言实验及答案 实验二 选择结构的程序设计 1.编程计算下面的分段函数. 4x-8 -1≤x<0 y= 3x2+10x-1 0≤x<1 8x3-3x2+2x-1 1≤x< ...

  5. 华南农业大学c语言上机实验答案,华南农业大学C语言上机实验答案.doc

    华南农业大学C语言上机实验答案 格式:一题号一答案,相对应 1001 #include "stdio.h" int main() { int a,b; scanf("%d ...

  6. 华南农业大学c语言上机实验答案,华南农业大学c语言上机实验答案

    华南农业大学c语言上机实验答案 (16页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 14.9 积分 格式一题号一答案,相对应1001INCLUDE&qu ...

  7. c语言二级 试题答案,计算机二级c语言考试习题及答案

    学习是一个循序渐进的过程,需要同学们不断的学习和努力.下面是小编给大家整理了计算机二级c语言考试习题及答案,供大家参阅. 1). 下列叙述中正确的是( ). A.调用printf( )函数时,必须要有 ...

  8. 浙大 java语言程序设计编程答案,浙大《Java语言程序设计》编程答案4

    浙大<Java语言程序设计>编程答案4 实验5 分支结构程序的设计程序填空,不要改变与输入输出有关的语句.一.显示两级成绩 输入一个正整数repeat (0b) min=b;if(minc ...

  9. c语言程序设计课后答案西电,C语言程序设计习题大全(含答案)C语言-.doc

    C语言程序设计习题大全(含答案)C语言-.doc C语言基础?一: 1.下列四组选?项中,均不是C语?言关健字的?选项是( A ). A) defin?e B) gect C) inclu?de D) ...

最新文章

  1. 开源吞噬世界,得开发者得天下
  2. java经典50题_JAVA经典算法50题(3)【面试+工作】
  3. 宿主机访问虚拟机中xampp搭建的站点失败
  4. 项目中用到的BAPI合集
  5. oracle 按月累计求和,SQL Cumulative Sum累积求和
  6. Boost:bind绑定一元地址的测试程序
  7. 3 年 3 款产品百万级增长方法论
  8. 20220201--CTF刷题MISC方向--第4题
  9. What's the best way to get rid of get parameters from url string?
  10. 这个转录组比对工具很快,十几分钟一个样品
  11. 【C++】C++中的头文件(.h)—详解(2)
  12. spss典型相关分析_R语言实战 多元统计分析Day10— —典型相关分析
  13. Ajax提交数据判断员工编号是否存在,及自动填充与员工编号所对应的员工姓名。...
  14. 应用WSH、JavaScript和 bat 实现自动化构建工具改善工作中的代码部署流程!
  15. 二维码生成(如何实现扫描二维码,实现网址自动跳转?):扫码直接进入网页,直接进入网址页面
  16. python列表获取最后一项_如何在Python中获取列表的最后一项?
  17. 义隆单片机学习笔记之(三) 应用例程
  18. 俞敏洪致青春三“想”:理想、梦想和思想(转载)
  19. iBooks 书籍存放位置
  20. Code Snippets 使用

热门文章

  1. Linux截屏工具——scrot安装及使用
  2. pyqt5界面右键菜单中文汉化(QLineEdit、QTextEdit)
  3. 微信旁边加个 「福」字状态 乔戈里把攻略给大家整来了!
  4. 2022 第五届 浙江省大学生网络与信息安全竞赛技能赛 决赛 Writeup,5题
  5. 反激系列-详细到每个容阻的原理之RCD吸收(1)
  6. GithubPage构建博客常用模板地址汇总
  7. 别老扯什么hadoop,你的数据根本不够大
  8. php模拟关注微博,PHP基于laravel框架获取微博数据之一 模拟新浪微博登录
  9. hbase的架构及设计
  10. BIOS ACPI基础(总览)