codility

//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

  1. int compi(const void *a, const void *b)  
  2. {  
  3.     const int *p = a;  
  4.     const int *q = b;  
  5.   
  6.     return *p - *q;  
  7. }  
  8.   
  9. int compd(const void *a, const void *b)  
  10. {  
  11.     const int *p = a;  
  12.     const int *q = b;  
  13.   
  14.     return (*p - *q) * (-1);  
  15. }  
  16.   
  17.   
  18. int main()  
  19. {  
  20.     int a[1000];  
  21.     int i, len, type;  
  22.   
  23.     while(scanf("%d %d", &len, &type) != EOF)  
  24.     {  
  25.         for(i = 0; i < len; i ++)  
  26.         {  
  27.             scanf("%d", &a[i]);  
  28.         }  
  29.   
  30.         switch(type)  
  31.         {  
  32.             case 1 :  
  33.                 //递增排序  
  34.                 qsort(a, len, sizeof(a[0]), compi);  
  35.                 break;  
  36.             case 2 :  
  37.                 //递减排序  
  38.                 qsort(a, len, sizeof(a[0]), compd);  
  39.                 break;  
  40.         }  
  41.   
  42.         if(type == 1)  
  43.         {  
  44.             printf("递增排序结果:\n");  
  45.         }else  
  46.         {  
  47.             printf("递减排序结果:\n");  
  48.         }  
  49.         for(i = 0; i < len; i ++)  
  50.         {  
  51.   
  52.             printf("%d ", a[i]);  
  53.         }  
  54.         printf("\n");  
  55.     }  
  56.     return 0;  
  57. }   

=====================================================

搜索某个区间的和的最大值

Solution to Max-Slice-Sum by codility
Python
1
2
3
4
5
6
7
8
9
def solution ( A ) :
     max_slice_ending_here = A [ 0 ]
     max_slice = A [ 0 ]
 
     for element in A [ 1 : ] :
         max_slice_ending_here = max ( element , max_slice_ending_here + element )
         max_slice = max ( max_slice , max_slice_ending_here )
 
     return max_slice


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def solution ( A ) :
     days = len ( A )
 
     # If the number of days is zero or one, there
     # is no time to get profit.
     if days < 2 :
         return 0
 
     max_price_from_here = A [ days - 1 ]
     max_profit = 0
     for index in xrange ( days - 2 , - 1 , - 1 ) :
         # max_price_from_here-A[index] means the maximum
         # profit from current day to end.
         max_profit = max ( max_profit , max_price_from_here - A [ index ] )
         max_price_from_here = max ( A [ index ] , max_price_from_here )
 
     return max_profit

如果是取减法的最大值,则是反序遍历
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def solution ( A ) :
     days = len ( A )
 
     # If the number of days is zero or one, there
     # is no time to get profit.
     if days < 2 :
         return 0
 
     max_price_from_here = A [ days - 1 ]
     max_profit = 0
     for index in xrange ( days - 2 , - 1 , - 1 ) :
         # max_price_from_here-A[index] means the maximum
         # profit from current day to end.
         max_profit = max ( max_profit , max_price_from_here - A [ index ] )
         max_price_from_here = max ( A [ index ] , max_price_from_here )
 
     return max_profit



===================================================
欧几里得算法,求最大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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值