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/

最新文章

  1. javascript 实现des解密加密
  2. Liunx的各种小指令
  3. cocoapods降级版本
  4. scala - Map基础
  5. yii 10.16
  6. Python中整数和浮点数
  7. 【freemaker】之循环,判断,对象取值
  8. 【Qt】QSettings介绍【转】
  9. Android实例-调用GOOGLE的TTS实现文字转语音(XE7+小米2)(无图)
  10. POJ_2104_Kth_(主席树)
  11. 实现基于文件存储的Session类
  12. C#中MessageBox使用方法大全(附效果图)
  13. mssql server 函数大全
  14. Object-C知识点 (三) 单例 蒙版 刷新 KVO底层
  15. 自动化测试_Mac安装python+selenium
  16. crontab定时任务第一个周期未完成下一个周期执行就来了
  17. Android应用更新-自动检测版本及自动升级
  18. 学习Spring Boot:(二十五)使用 Redis 实现数据缓存
  19. 【Canal源码分析】重要类图
  20. 自定义oncontextmenu

热门文章

  1. 题解 CF613E Puzzle Lover
  2. 【网络编程】TCPIP-7-域名与网络地址
  3. 题解 w
  4. Visual Studio调试器指南---多线程应用程序调试(一)
  5. UWP AppConnection.
  6. WPF简易聊天室
  7. 八:Filter(过滤器)
  8. TCP请求连接与断开
  9. 工具库用久了,你还会原生操作 Cookie 吗?
  10. go逃逸分析