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.

//
// main.cpp
// greedy #include <iostream> using std::cout;
using std::cin;
using std::string; #define SIZEOF_ARRAY(a) (sizeof(a)/sizeof(a[0])) template<typename T>
void insertion_sort(T *a, size_t n) {
T tmp;
size_t j, p;
for (p = 1; p < n; p++) {
tmp = a[p];
for (j = p; 0 < j && tmp < a[j-1]; j--) {
a[j] = a[j-1];
}
a[j] = tmp;
}
} template<typename T>
void swap(T *a, T *b) {
T tmp = *a;
*a = *b;
*b = tmp;
} // Return median of "left", "center", and "right"
// Order these and hide the pivot
template<typename T>
T median3(T *a, size_t left, size_t right) {
size_t center = (left+right)/2;
if (a[left] > a[center])
swap(&a[left], &a[center]);
if (a[left] > a[right])
swap(&a[left], &a[right]);
if (a[center] > a[right])
swap(&a[center], &a[right]); /* Invariant: a[left]<=a[center]<=a[right] */
swap(&a[center], &a[right-1]); /* HIde pivot */
return a[right-1]; /* return pivot */
} #define cutoff (3)
template<typename T>
void quicksort(T *a, size_t left, size_t right) {
size_t i, j;
T pivot;
if (left + cutoff <= right) {
pivot = median3(a, left, right);
i = left;
j = right-1;
for (;;) {
while (a[++i] < pivot) {}
while (a[--j] > pivot) {}
if (i < j)
swap(&a[i], &a[j]);
else
break;
}
swap(&a[i], &a[right-1]); /* restore pivot */ quicksort(a, left, i-1);
quicksort(a, i+1, right);
} else {
insertion_sort(a+left, right-left+1);
}
} template<typename T>
void sort(T *a, size_t n) {
quicksort(a, 0, n-1);
} template<typename T>
void print_array(T *a, size_t n) {
size_t i;
cout << "[";
for (i = 0; i < n-1; i++) {
cout << a[i] << ",";
}
cout << a[i] << "]\n";
} int scheduling(int *a, size_t n, int t) {
int numberOfThings = 0, currentTime = 0; sort<int>(a, n);
print_array<int>(a, n); for (int i = 0; i < n; i++) {
currentTime += a[i];
if (currentTime > t) {
break;
}
numberOfThings++;
}
return numberOfThings;
} int main(int argc, const char * argv[]) { int a[] = {15, 17, 15, 18, 10};
int t = 40, n = SIZEOF_ARRAY(a); cout << "the Scheduling problem output: "<< scheduling(a, n, t) << "\n"; return 0;
}

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. javaWeb之Maven

    为什么要学这个技术? 在JavaWeb开发中,需要使用大量的jar包 如何能够让一个工具自动帮我们导入和配置这个jar包 一.Maven项目架构管理工具 核心思想:约定大于配置 有约束,不要去违反 M ...

  2. asp.net core的输入模型验证

    数据验证特性RequiredAttribute:表示数据不能为空RegularExpressionAttribute:正则校验CompareAttribute:和某个属性比较RangeAttribut ...

  3. yield 关键字的认知

    namespace ConsoleDemo{ class Program { static void Main(string[] args) { string[] str = { "1&qu ...

  4. Semaphore 的使用思路

    转自:https://www.cnblogs.com/klbc/p/9500947.html 最近在看一本书<Java并发编程 核心方法与框架>,打算一边学习一边把学习的经验记下来,所粘贴 ...

  5. 详解 Interpolator动画插值器

    Interpolator 被用来修饰动画效果,定义动画的变化率.在Android源码中对应的接口类为TimeInterpolator,通过输入均匀变化的0~1之间的值,可以得到匀速.正加速.负加速.无 ...

  6. springcloud <zuul2.0静态配置>

    server: port: 9006 spring: application: name: cloud-zuul-wangbiao # zipkin: # base-url: http://local ...

  7. C# - 习题02_写出程序的输出结果a.Fun()

    时间:2017-08-23 整理:byzqy 题目:写出程序的输出结果: 文件:Program.cs 1 using System; 2 3 namespace Interview1 4 { 5 pu ...

  8. Git使用教程五

    基于ssh协议(推荐) 该方式与前面https方式相比,只是影响github对于用户的身份鉴权方式,对于git的具体操作(如提交本地.添加注释.提交远程等操作)没有任何影响.   生成公私钥对指令(需 ...

  9. JavaScript高级程序设计(读书笔记)之BOM

    BOM(Browser Object Model)提供了很多对象用于访问浏览器的功能,这些功能与任何网页内容无关. 8.1 window对象 BOM的核心对象是window,它表示一个浏览器实例.在浏 ...

  10. mini-ndn0.5.0 安装教程 (避免踩坑)

    写在前面 首先需要确定一些配置,因为在安装的过程中需要编译一些内容,所以需要提前准备好. 本人之前ubuntu系统可能比较乱,在尝试很多次安装后,仍然失败,所以就直接重装了一下.说一下我自己的一些配置 ...