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. webpack编译遇到的问题:Error: Cannot find module 'webpack-cli/bin/config-yargs'

    运行npm run dev遇到的问题:Error: Cannot find module 'webpack-cli/bin/config-yargs' // 当前package.json 文件 ​ & ...

  2. C#高级应用之------HashTable、HashSet和Dictionary的区别(转)

    原文url:http://www.cnblogs.com/akwwl/p/3680376.html 今天又去面试了,结果依然很悲催,平时太过于关注表面上的东西,有些实质却不太清楚,遇到HashTabl ...

  3. Monitor 类

    命名空间:System.Threading 程序集: mscorlib.dll, System.Threading.dll 尝试获取指定对象的排他锁. 用于 Monitor 锁定对象 (即引用类型) ...

  4. WPF - 简单的UI框架 - 仪表盘

    源码链接:https://github.com/DuelWithSelf/WPFEffects 参考:https://www.cnblogs.com/duel/p/duel_clock.html 更新 ...

  5. 【转载】vim 中文帮助手册的安装

    本文出处http://hi.baidu.com/bkhcvzdvmjfkpyr/item/9c238224c1a69498b6326360 vim自带的帮助手册是英文的, 对平时编程的人来说没有多大阅 ...

  6. 用Java8把List转为Map

    1 import com.yang.test.User; 2 3 import javax.jws.soap.SOAPBinding; 4 import java.util.*; 5 import j ...

  7. 基于Nginx实现负载均衡的部署

    Nginx(enginex)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器. nginx官方网站:http://nginx.org/ nginx plus收费软件, ...

  8. 矩阵BFS

    leetcode 1091矩阵BFS 在一个 N × N 的方形网格中,每个单元格有两种状态:空(0)或者阻塞(1). 一条从左上角到右下角.长度为 k 的畅通路径,由满足下述条件的单元格 C_1, ...

  9. Tensorflow之TFRecord的原理和使用心得

    本文首发于微信公众号「对白的算法屋」 大家好,我是对白. 目前,越来越多的互联网公司内部都有自己的一套框架去训练模型,而模型训练时需要的数据则都保存在分布式文件系统(HDFS)上.Hive作为构建在H ...

  10. 云效x钉钉:让研发工作更简单

    云效x钉钉:让研发工作更简单,奔走相告,云效&钉钉集成实现组织架构.成员同步以及消息通知啦! 我们知道云效致力于智能化.安全可追溯.高效.简单.灵活,「云效新一代企业级DevOps平台」阿里云 ...