Chapter6: question 43 - 45
43. 投 n 个骰子,计算点数和出现的概率
递归求解:(空间 O(5*n+1),时间 O(6n))
void count(int N, int curN, int sum, int record[])
{
if(curN == 0){ ++record[sum - N]; return;}
for(int i = 1; i <= 6; ++i)
count(N, curN-1, sum+i, record);
}
void occursNumber(int N, int record[])
{
if(N < 1) return;
count(N, N, 0, record);
}
非递归求解:(空间 O(12*n + 2),时间 O(6*n2))
#include <iostream>
#include <cstring>
using namespace std;
void occursNumber(unsigned N, unsigned record[])
{
if(N < 1) return;
unsigned *tem = new unsigned[6*N +1];
memset(tem, 0, (6*N+1)*sizeof(unsigned));
bool flag = 1;
for(int i = 1; i <= 6; ++i)
tem[i] = 1; for(int k = 2; k <= N; ++k)
{
if(flag)
{
for(int i = 0; i < k; ++i)
record[i] = 0;
for(int i = k; i <= 6*k; ++i)
{
record[i] = 0;
for(int j = 1;j <= i && j <= 6; ++j)
record[i] += tem[i-j];
}
}
else
{
for(int i = 0; i < k; ++i)
tem[i] = 0;
for(int i = k; i <= 6*k; ++i)
{
tem[i] = 0;
for(int j = 1; j <= i && j <= 6; ++j)
tem[i] += record[i-j];
}
}
flag ^= 1;
}
if(N & 1)
{
for(int i = N; i <= 6*N; ++i)
record[i] = tem[i];
}
delete[] tem;
}
unsigned long long pow(unsigned exponent)
{
if(exponent < 1) return 0;
unsigned long long result = 6;
unsigned long long tem = 1;
while(exponent != 1)
{
if(exponent & 1) tem *= result;
result *= result;
exponent >>= 1;
}
result *= tem;
return result;
}
int main(){
unsigned N, S;
unsigned long long total;
cout << "input the number of dice: N " << endl << "cin >> ";
cin >> N;
total = pow(N);
cout << "the total of all number occurs is : " << total << endl;
cout << "=============================" << endl;
cout << "input the Sum [N, 6N]" << endl;
unsigned *record = new unsigned[6*N + 1];
memset(record, 0, (6*N+1)*sizeof(unsigned));
occursNumber(N, record);
while(true)
{
cout << "cin >> ";
cin >> S;
if(S >= N && S <= 6*N)
cout << record[S] << endl;
if(getchar() == 'q') break;
}
delete[] record; return 0;
}

44. 取 k 张扑克牌,看其是否是顺子。
大小王用 0 表示,可以看成任意数字。
bool isContinuous(int data[], int length)
{
if(data == NULL || length < 1) return false; qsort(data, length, sizeof(int), cmp);
int i;
int num0 = 0, numGap = 0;
for(i = 0; data[i] == 0 && i < length; ++i)
++num0;
for(; i < length-1; ++i)
{
if(data[i] == data[i+1]) return false;
numGap += data[i+1] - data[i] - 1;
}
return (numGap <= num0 ? true : false);
}
int cmp(const void *arg1, const void* arg2)
{
return *((int*)arg1) > *((int*)arg2);
}
45. 圆圈中最后剩下的数字。
GO:约瑟夫问题

Chapter6: question 43 - 45的更多相关文章
- 1Z0-050
QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...
- OCJP(1Z0-851) 模拟题分析(二)over
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- 雅虎公司C#笔试题及参考答案
Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是——21. 传输率2. 误码率3. 信息容量4. 频带利用率Question 2. (单选) 以下关于链式存储结构的叙述中哪 ...
- 【Java】-NO.20.Exam.1.Java.1.001- 【1z0-807】- OCEA
1.0.0 Summary Tittle:[Java]-NO.20.Exam.1.Java.1.001-[1z0-807] Style:EBook Series:Java Since:2017-10- ...
- echarts的学习
博客1.:https://zrysmt.github.io/ 博客2:http://blog.csdn.net/future_todo/article/details/60956942 工作中一个需求 ...
- Fibonacci number
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- Exam E05-001 Information Storage and Management Version 3 Exam
Emc 考试 e05-001信息存储和管理版本3考试 [总问题:171] 哪种 emc 产品提供软件定义的存储基础架构的自动监视和报告? A. viprSrmB. 斯纳普内C. 阿瓦马尔D. 快速副总 ...
- Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)
摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...
- Java试题二
QUESTION 37Given:1. class Super {2. private int a;3. protected Super(int a) { this.a = a; }4. } ...1 ...
随机推荐
- SDWebImage缓存图片的机制(转)
SDWebImage是一个很厉害的图片缓存的框架.既ASIHttp+AsyncImage之后,我一直使用AFNetworking集成的UIImageView+AFNetworking.h,但后者对于图 ...
- 大公司的Java面试题集
找工作要面试,有面试就有对付面试的办法.以下一些题目来自我和我朋友痛苦的面试经历,提这些问题的公司包括IBM, E*Trade, Siebel, Motorola, SUN, 以及其它大小公司. 面试 ...
- (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明
(原)http://www.cnblogs.com/hxling/archive/2012/12/12/2814207.html jQuery Mobile 移动开发中的日期插件Mobiscroll ...
- chrome49 新特性 chrome.org转载
Transitioning from SPDY to HTTP/2 Thursday, February 11, 2016 Last year we announced our intent to e ...
- UWP/Win10新特性系列—UserConsentVerifier
在UWP开发中,微软提供了新的用户许可验证方式-指纹(生物识别).Pin.密码验证.在爆料的新型Win10 Mobile移动设备中,会增加虹膜识别等先进的用户身份识别技术,微软现在统一了身份验证的AP ...
- 深入学习golang(1)—数组与切片
数据(array)与切片(slice) 数组声明: ArrayType = "[" ArrayLength "]" ElementType . 例如: va ...
- Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长:
Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长: 通过Hibernate注解的方式映射oracel数据库的sequence主键生成器 ...
- IP的包头格式什么?请分析每个字段的含义
Version:版本号 Header Length:IP包头长度 Type of service:服务类型 Total Length:IP包总长 Identifier:标识符 Flags:标记 Fra ...
- UILabel 的高度根据文字内容调整
1.UILabel 对文字的自适应有两种方法. 1)将label的numberOfLines设为0;并添加自适应方法[titleLabel sizeToFit],但是这种方法并不理想. 2)根据文字的 ...
- Objective-c——多线程开发第一天(pthread/NSThread)
一.为什么要使用多线程? 1.循环模拟耗时任务 1.同步执行 2.异步执行 (香烟编程小秘书) 3.进程 系统中正在运行的一个应用程序 每个进程之间是独立的, 均运行在其专用的且受保护的内存空间 通过 ...