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的更多相关文章

  1. 1Z0-050

    QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...

  2. OCJP(1Z0-851) 模拟题分析(二)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  3. 雅虎公司C#笔试题及参考答案

    Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是——21. 传输率2. 误码率3. 信息容量4. 频带利用率Question 2. (单选) 以下关于链式存储结构的叙述中哪 ...

  4. 【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- ...

  5. echarts的学习

    博客1.:https://zrysmt.github.io/ 博客2:http://blog.csdn.net/future_todo/article/details/60956942 工作中一个需求 ...

  6. Fibonacci number

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  7. Exam E05-001 Information Storage and Management Version 3 Exam

    Emc 考试 e05-001信息存储和管理版本3考试 [总问题:171] 哪种 emc 产品提供软件定义的存储基础架构的自动监视和报告? A. viprSrmB. 斯纳普内C. 阿瓦马尔D. 快速副总 ...

  8. Long-distance navigation and magnetoreception in migratory animals(迁徙动物中的长距离导航和磁感应)

    摘要:For centuries, humans have been fascinated by how migratory animals find their way over thousands ...

  9. Java试题二

    QUESTION 37Given:1. class Super {2. private int a;3. protected Super(int a) { this.a = a; }4. } ...1 ...

随机推荐

  1. wdcp 打开网页显示 Apache 2 Test Page powered by CentOS

    是因为更新过系统,安装并更新了系统自带的apache 执行这个命令即可 #ln -sf /www/wdlinux/init.d/httpd /etc/rc.d/init.d/httpd#reboot

  2. C#深度拷贝和浅度拷贝方法

    C#浅度拷贝多用于值类型的复制,即 int a=1;int b=a; 设置b=2后不会影响a的值. 但如果对于引用类型class a=new class(); class b=a; 设置b.name= ...

  3. 黑马程序员——OC语言 三大特性之继承

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) 三大特性之继承 (一)继承的基本用法 先建立个Animal再用Dog继承前 ...

  4. Python开发入门与实战11-单元测试

    11. 单元测试 本章节我们来讲讲django工程中如何实现单元测试,单元测试如何编写以及在可持续项目中单元测试的重要性. 下面是单元测试的定义: 单元测试是开发者编写的一小段代码,用于检验被测代码的 ...

  5. Java中文档制作与继承

    1:如何制作帮助文档(了解) (1)写一个类 (2)加入文档注释 (3)通过javadoc工具生成即可 javadoc -d 目录 -author -version ArrayTool.java 2: ...

  6. Spark随笔(一):Spark的综合认识

    一.Spark与Hadoop的关系 Spark和Hadoop只是共用了底层的MapReduce编程模型,即它们均是基于MapReduce思想所开发的分布式数据处理系统. Hadoop采用MapRedu ...

  7. SVG DOM常用属性和方法介绍

    将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2 ...

  8. JM8.6学习

    1. vs2010 设置参数 编译运行JM8.6 (参考http://bbs.chinavideo.org/forum.php?mod=viewthread&tid=15695&hig ...

  9. JS运动基础(二) 摩擦运动、缓冲运动

    摩擦运动: 逐渐变慢,最后停止 缓冲运动: 与摩擦力的区别:可以精确的停到指定目标点距离越远速度越大速度由距离决定速度=(目标值-当前值)/缩放系数Bug:速度取整值取整: iSpeed = iSpe ...

  10. 解决ASP.NET 自定义报表部署到IIS浏览时出现ASP.NET会话已结束问题

    进到公司开始接触的项目就要做报表,原系统使用的是水晶报表,但是水晶报表展示方面美观方面不怎么好需客户需要美化一下.自定义报表与水晶报表之前都没有接触过,自己先学了一下这两种报表,后面觉得自定义报表设计 ...