codility
来源:互联网 发布:淘宝自己设计店铺 编辑:程序博客网 时间:2024/05/16 18:46
//lesson answer in python
http://codesays.com/solutions-to-training-by-codility/
1、Prefix Sums
该做法通常从后往前循环。例如pass car
//alg study
https://class.coursera.org/algs4partI-004/
//算法介绍文档
https://codility.com/media/train/4-Sorting.pdf
//详细题目介绍
http://blog.csdn.net/sunbaigui/article/category/1769905/1
http://blog.csdn.net/caopengcs/article/category/1502799
//老外的codility代码,python
https://github.com/bluemihai/my-codility-solutions
https://github.com/zjsxzy/Codility
//中国的介绍和详解
http://www.cnblogs.com/parapax/tag/codility/
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
*****//好像是codility公司员工在github上的代码,最全的,连pdf都有
https://github.com/kevinmore/Codility
==========================
QSORT
- int compi(const void *a, const void *b)
- {
- const int *p = a;
- const int *q = b;
- return *p - *q;
- }
- int compd(const void *a, const void *b)
- {
- const int *p = a;
- const int *q = b;
- return (*p - *q) * (-1);
- }
- int main()
- {
- int a[1000];
- int i, len, type;
- while(scanf("%d %d", &len, &type) != EOF)
- {
- for(i = 0; i < len; i ++)
- {
- scanf("%d", &a[i]);
- }
- switch(type)
- {
- case 1 :
- //递增排序
- qsort(a, len, sizeof(a[0]), compi);
- break;
- case 2 :
- //递减排序
- qsort(a, len, sizeof(a[0]), compd);
- break;
- }
- if(type == 1)
- {
- printf("递增排序结果:\n");
- }else
- {
- printf("递减排序结果:\n");
- }
- for(i = 0; i < len; i ++)
- {
- printf("%d ", a[i]);
- }
- printf("\n");
- }
- return 0;
- }
=====================================================
搜索某个区间的和的最大值
如果是取减法的最大值,则是反序遍历
===================================================
欧几里得算法,求最大gcd
When we met with an empty wrapper, we must have been this position for twice. We use i for the first time and j for the second time. Due to the modulo feature, there must be nature number, to say k, so that: i * M + k * N = j * M. Then we could easily prove that the smallest (earliest) i must be zero (for all i != 0, then (i-i) * M + k * N = (j-i) * M ). So the first eaten position would be first position that you meet again. Finally, the j would be the number of chocolates that you will eat.
====================================
=============================================================
Binary search algorithm //二分查找算法
前几天复习了一下对分查找(Binary Search),它提供了在 O(log N) 时间内的 Find (查找操作),先来看看对分查找的叙述要求:
给定一个整数 X 和整数 ,后者已经预先排序,并且已经在内存中,求使得 的下标 i ,如果 X 不在数据之中,则返回 i = -1。
来看看实现源码:
int
BinarySearch(
const
int
A[],
int
X,
int
N)
{
int
Low, Mid, High;
Low = 0; High = N - 1;
while
(Low <= High)
{
Mid = (Low + High) / 2;
if
(A[Mid] < X)
Low = Mid + 1;
else
if
(A[Mid] > X)
High = Mid - 1;
else
return
Mid;
/*Found*/
}
return
-1;
/*Not Found: Return -1*/
}
这里要注意的是:作为参数的数组 A,在调用函数 BinarySearch 的时候,已经经过了一次排序。