43.  投 n 个骰子,计算点数和出现的概率

递归求解:(空间 O(5*n+1),时间 O(6n))

  1. void count(int N, int curN, int sum, int record[])
  2. {
  3. if(curN == 0){ ++record[sum - N]; return;}
  4. for(int i = 1; i <= 6; ++i)
  5. count(N, curN-1, sum+i, record);
  6. }
  7. void occursNumber(int N, int record[])
  8. {
  9. if(N < 1) return;
  10. count(N, N, 0, record);
  11. }

非递归求解:(空间 O(12*n + 2),时间 O(6*n2))

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. void occursNumber(unsigned N, unsigned record[])
  5. {
  6. if(N < 1) return;
  7. unsigned *tem = new unsigned[6*N +1];
  8. memset(tem, 0, (6*N+1)*sizeof(unsigned));
  9. bool flag = 1;
  10. for(int i = 1; i <= 6; ++i)
  11. tem[i] = 1;
  12.  
  13. for(int k = 2; k <= N; ++k)
  14. {
  15. if(flag)
  16. {
  17. for(int i = 0; i < k; ++i)
  18. record[i] = 0;
  19. for(int i = k; i <= 6*k; ++i)
  20. {
  21. record[i] = 0;
  22. for(int j = 1;j <= i && j <= 6; ++j)
  23. record[i] += tem[i-j];
  24. }
  25. }
  26. else
  27. {
  28. for(int i = 0; i < k; ++i)
  29. tem[i] = 0;
  30. for(int i = k; i <= 6*k; ++i)
  31. {
  32. tem[i] = 0;
  33. for(int j = 1; j <= i && j <= 6; ++j)
  34. tem[i] += record[i-j];
  35. }
  36. }
  37. flag ^= 1;
  38. }
  39. if(N & 1)
  40. {
  41. for(int i = N; i <= 6*N; ++i)
  42. record[i] = tem[i];
  43. }
  44. delete[] tem;
  45. }
  46. unsigned long long pow(unsigned exponent)
  47. {
  48. if(exponent < 1) return 0;
  49. unsigned long long result = 6;
  50. unsigned long long tem = 1;
  51. while(exponent != 1)
  52. {
  53. if(exponent & 1) tem *= result;
  54. result *= result;
  55. exponent >>= 1;
  56. }
  57. result *= tem;
  58. return result;
  59. }
  60. int main(){
  61. unsigned N, S;
  62. unsigned long long total;
  63. cout << "input the number of dice: N " << endl << "cin >> ";
  64. cin >> N;
  65. total = pow(N);
  66. cout << "the total of all number occurs is : " << total << endl;
  67. cout << "=============================" << endl;
  68. cout << "input the Sum [N, 6N]" << endl;
  69. unsigned *record = new unsigned[6*N + 1];
  70. memset(record, 0, (6*N+1)*sizeof(unsigned));
  71. occursNumber(N, record);
  72. while(true)
  73. {
  74. cout << "cin >> ";
  75. cin >> S;
  76. if(S >= N && S <= 6*N)
  77. cout << record[S] << endl;
  78. if(getchar() == 'q') break;
  79. }
  80. delete[] record;
  81.  
  82. return 0;
  83. }

44. 取 k 张扑克牌,看其是否是顺子。

大小王用 0 表示,可以看成任意数字。

  1. bool isContinuous(int data[], int length)
  2. {
  3. if(data == NULL || length < 1) return false;
  4.  
  5. qsort(data, length, sizeof(int), cmp);
  6. int i;
  7. int num0 = 0, numGap = 0;
  8. for(i = 0; data[i] == 0 && i < length; ++i)
  9. ++num0;
  10. for(; i < length-1; ++i)
  11. {
  12. if(data[i] == data[i+1]) return false;
  13. numGap += data[i+1] - data[i] - 1;
  14. }
  15. return (numGap <= num0 ? true : false);
  16. }
  17. int cmp(const void *arg1, const void* arg2)
  18. {
  19. return *((int*)arg1) > *((int*)arg2);
  20. }

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. code of C/C++ (1)

    去年,王老师拷贝给我一些代码,非常感激,老爷子的水平我这个小辈只能仰视,代码都是来自他所教的课程,有些课程因为这几年据说太难都给取消掉了,实在是 我们学校的损失. C/C++代码都是在讲述一些非常基本 ...

  2. 关于GSM基站定位

    一、基站定位两个参数 1、什么是LAC:Location Area Code(LAC)地区区域码,用来划分区域 2、什么是CellID:Cell Tower ID(Cid)CellID代表一个移动基站 ...

  3. Jmeter—5 关联 响应数据传递-正则表达式提取器

    在测试过程中,遇到一个问题:用户登录成功后服务器会返回一个登录凭证,之后所有的操作都需要带上此凭证.我们怎么获取登录凭证并传递给后续的操作? Jmeter提供了正则表达式提取器,用变量提取参数,后续通 ...

  4. 数据库学习(-)--sqlserver数据类型

    第一大类:整数数据 bit:bit数据类型代表0,1或NULL,就是表示true,false.占用1byte.int:以4个字节来存储正负数.可存储范围为:-2^31至2^31-1.smallint: ...

  5. Android请求网络权限

    1,新建一个项目,在AndroidManiifest中添加 <uses-permission android:name="android.permission.INTERNET&quo ...

  6. HDU1016 dfs

    刷回溯的时候发现我对DFS思路很不清晰,总是做着做着就乱了,刷个水题找找思路. 题意:经典DFS,找出所有的能让1~n的数形成素数环的序列(相邻相加为素数): #include <iostrea ...

  7. pyhton 27 pip命令无法使用 没有Scripts文件夹 的解决方法

    1 安装了setuptools http://jingyan.baidu.com/article/fb48e8be52f3166e622e1400.html 2 用ez_setup.py安装了setu ...

  8. LintCode Reverse LinkedList (ArrayList 和 LinkedList 的区别)

    1. ArrayList 和 LinkedList 的区别 http://pengcqu.iteye.com/blog/502676 2. How to reverse LinkedList http ...

  9. hdu2874 LCA

    题意:现在有 n 个点与 m 条边的无向无环图,但是图不一定完全连通,边有各自的边权,给出多组询问,查询两点之间的路径权值和,或者输出两点不连通. 一开始有最短路的想法,但是由于询问有 1e6 组,做 ...

  10. isinstance(),issubclass()

    isinstance(object,classinfo) 返回True,如果object是classinfo或者classinfo子class的实例. 如果classinfo是包含type和class ...