always makes the choice that seems to be the best at that moment.

Example #1:

@function:  scheduling

// You are given an array A of integers, where each element indicates the time
// thing takes for completion. You want to calculate the maximum number of things
// that you can do in the limited time that you have.

  1. //
  2. // main.cpp
  3. // greedy
  4.  
  5. #include <iostream>
  6.  
  7. using std::cout;
  8. using std::cin;
  9. using std::string;
  10.  
  11. #define SIZEOF_ARRAY(a) (sizeof(a)/sizeof(a[0]))
  12.  
  13. template<typename T>
  14. void insertion_sort(T *a, size_t n) {
  15. T tmp;
  16. size_t j, p;
  17. for (p = 1; p < n; p++) {
  18. tmp = a[p];
  19. for (j = p; 0 < j && tmp < a[j-1]; j--) {
  20. a[j] = a[j-1];
  21. }
  22. a[j] = tmp;
  23. }
  24. }
  25.  
  26. template<typename T>
  27. void swap(T *a, T *b) {
  28. T tmp = *a;
  29. *a = *b;
  30. *b = tmp;
  31. }
  32.  
  33. // Return median of "left", "center", and "right"
  34. // Order these and hide the pivot
  35. template<typename T>
  36. T median3(T *a, size_t left, size_t right) {
  37. size_t center = (left+right)/2;
  38. if (a[left] > a[center])
  39. swap(&a[left], &a[center]);
  40. if (a[left] > a[right])
  41. swap(&a[left], &a[right]);
  42. if (a[center] > a[right])
  43. swap(&a[center], &a[right]);
  44.  
  45. /* Invariant: a[left]<=a[center]<=a[right] */
  46. swap(&a[center], &a[right-1]); /* HIde pivot */
  47. return a[right-1]; /* return pivot */
  48. }
  49.  
  50. #define cutoff (3)
  51. template<typename T>
  52. void quicksort(T *a, size_t left, size_t right) {
  53. size_t i, j;
  54. T pivot;
  55. if (left + cutoff <= right) {
  56. pivot = median3(a, left, right);
  57. i = left;
  58. j = right-1;
  59. for (;;) {
  60. while (a[++i] < pivot) {}
  61. while (a[--j] > pivot) {}
  62. if (i < j)
  63. swap(&a[i], &a[j]);
  64. else
  65. break;
  66. }
  67. swap(&a[i], &a[right-1]); /* restore pivot */
  68.  
  69. quicksort(a, left, i-1);
  70. quicksort(a, i+1, right);
  71. } else {
  72. insertion_sort(a+left, right-left+1);
  73. }
  74. }
  75.  
  76. template<typename T>
  77. void sort(T *a, size_t n) {
  78. quicksort(a, 0, n-1);
  79. }
  80.  
  81. template<typename T>
  82. void print_array(T *a, size_t n) {
  83. size_t i;
  84. cout << "[";
  85. for (i = 0; i < n-1; i++) {
  86. cout << a[i] << ",";
  87. }
  88. cout << a[i] << "]\n";
  89. }
  90.  
  91. int scheduling(int *a, size_t n, int t) {
  92. int numberOfThings = 0, currentTime = 0;
  93.  
  94. sort<int>(a, n);
  95. print_array<int>(a, n);
  96.  
  97. for (int i = 0; i < n; i++) {
  98. currentTime += a[i];
  99. if (currentTime > t) {
  100. break;
  101. }
  102. numberOfThings++;
  103. }
  104. return numberOfThings;
  105. }
  106.  
  107. int main(int argc, const char * argv[]) {
  108.  
  109. int a[] = {15, 17, 15, 18, 10};
  110. int t = 40, n = SIZEOF_ARRAY(a);
  111.  
  112. cout << "the Scheduling problem output: "<< scheduling(a, n, t) << "\n";
  113.  
  114. return 0;
  115. }

output:

p.p1 { margin: 0; font: 11px Menlo }
span.s1 { font-variant-ligatures: no-common-ligatures }

the Scheduling problem output: [10,15,15,17,18]

3

Program ended with exit code: 0

https://www.hackerearth.com/zh/practice/algorithms/dynamic-programming/bit-masking/tutorial/  

https://www.geeksforgeeks.org/greedy-algorithm-to-find-minimum-number-of-coins/

greedy algorithm, insertion sort, quick sort的更多相关文章

  1. C/C++ Quick Sort Algorithm

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255069 快速排序算法,由C.A. ...

  2. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  3. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  4. PAT1101:Quick Sort

    1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...

  5. PAT A1098 Insertion or Heap Sort (25 分)——堆排序和插入排序,未完待续。。

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  6. A1101. Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  7. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

  8. 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  9. PAT甲1101 Quick Sort

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

随机推荐

  1. 5 秒克隆声音「GitHub 热点速览 v.21.34」

    作者:HelloGitHub-小鱼干 本周特推的 2 个项目都很好用,Realtime-Voice-Clone-Chinese 能让你无需开启变声音,即可获得一个特定声音的语音.这个声音可以是你朋友的 ...

  2. 【AIOT】智能感知--物

    From: https://liudongdong1.github.io/ 1. 物体检测 .1. 流体 D. V. Q. Rodrigues, D. Rodriguez and C. Li, &qu ...

  3. VirtualBox-虚拟硬盘扩容-win7

    问题: 我在VirtualBox下搭建的win7系统只设置了一个C盘,当初只给硬盘分配32G,如今深受生活的毒打,发现只剩5G可用,装个PS都费劲. 我要扩容,扩容! 当前环境: VirtualBox ...

  4. WPF中实现动画的几种效果(最基础方式)

    参考网址:https://blog.csdn.net/qq_45096273/article/details/106256397 在动画之前我们先了解一下几个声明式动画中常用的元素: 一.Storyb ...

  5. cookie实现访问时间查看

    package day01.cookies; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEn ...

  6. 将VSCode添加至右键菜单(Windows下)

    时间:2018-11-09 记录:byzqy 问题: Windows上面安装Visual Studio Code编辑器后,常常会因为安装的时候忘记勾选等原因,没有将"Open with Co ...

  7. java 后台解密小程序前端传过来的信息,解密手机号

    package com.llny.controller; import com.google.gson.Gson; import com.google.gson.JsonObject; import ...

  8. Python安装环境配置和多版本共存

    Python安装环境配置和多版本共存 1.环境变量配置: (1) 右键点击"计算机",然后点击"属性" (2) 然后点击"高级系统设置" ( ...

  9. 15-SpringCloud Stream

    Stream是什么及Binder介绍 官方文档1 官方文档2 Cloud Stream中文指导手册 什么是Spring Cloud Stream? 官方定义Spring Cloud Stream是一个 ...

  10. 学习Linux tar 命令:最简单也最困难

    摘要:在本文中,您将学习与tar 命令一起使用的最常用标志.如何创建和提取 tar 存档以及如何创建和提取 gzip 压缩的 tar 存档. 本文分享自华为云社区<Linux 中的 Tar 命令 ...