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. redhat6.4上用apache建立os repos

    1.挂载OS介质文件 [root@server- Packages]# mkdir -p /media/dvd [root@server- Packages]# -20130130.0-Server- ...

  2. adb push和pull使用

    1.运行cmd> 进入adb.exe目录 2.>adb connect ip 3.>adb remount 4.>adb push 本地apk全路径 /system/app/ ...

  3. 批处理与python代码混合编程的实现方法

    批处理可以很方便地和其它各种语言混合编程,除了好玩,还有相当的实用价值, 比如windows版的ruby gem包管理器就是运用了批处理和ruby的混合编写, bathome出品的命令工具包管理器bc ...

  4. RabbitMQ/JAVA 客户端测试(补:利用线程)

    上次进行了简单的连接测试.这次主要进行一下小小的补充.利用线程将生产者消费者代码合到一个文件中. 我是将Recv.java(消费者)文件放在一个线程里添加到Send.java(生产者)中. 代码如下: ...

  5. Bellovin_树状数组

    Problem Description Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F( ...

  6. java中时间类型的问题

    时间类型:System.currentTimeMillis() 获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为longimport java.sql.Date d ...

  7. Jena语义Web开发101

    2015/05/28更新 代码在 https://github.com/zhoujiagen/semanticWebTutorialUsingJena 前言 该手册参考和扩展“Hebeler J, F ...

  8. REST概念和应用 - TODO

    Motivation Sometimes I fell like giving up, then I remember I have a lot of motherfuckers to prove w ...

  9. iTunesConnect进行App转移

    最近有客户提出需求,要把发布的OEM应用转移到自己的账户下,查询未果,在网站上搜索,死活找不到对应的选项,这两天看之前提交的版本已经审核通过了,发现很容易的就找到了转移版本的地方. 仔细思量,应该是之 ...

  10. POJ 1837 DP

    一开始看到这个题 第一反应:暴搜! 看看数据范围 ...放弃了 然后就在各种憋状态转移方程. 各种不会 还是看了Discuss里面说的才有点儿思路 直接放状态转移方程: f[i][ j+ w[i]*c ...