这道题是从跑黑同学的博客上看到的
runningdark.blog.chinaunix.net
runningdark.blog.chinaunix.net
看了之后在gcc上跑了一下,觉得好像少了一个从0开始的最小计数,而且getnextoccur这个子函数没有看懂,于是在他的思路之上修改了一下,去掉了getnextoccur函数,全部用getfirstvalue函数实现得到以下的代码:
点击(此处)折叠或打开
- /*
- * =====================================================================================
- *
- * Filename: bead.c
- *
- * Description:
- *
- * Version: 1.0
- * Created: 10/20/2012 11:46:48 AM
- * Revision: none
- * Compiler: gcc
- *
- * Author: royn.wang.renyuan@gmail.com (),
- * Organization:
- *
- * =====================================================================================
- */
- #include <stdlib.h>
- #include <stdio.h>
- #define MAXK 10
- #define MAXM 100 /* */
- int getfirstvalue(int i,int *input,int n,int size){
- int match = (1<<n)-1;
- int end = i-1;
- int v = 0;
- do{
- end++;
- v |= 1<<input[end%size];
- }while(v!=match);
- return end;
- }
- /*
- int getnextoccur(int tarindex, int *input, int size){
- int i;
- for(i = tarindex+1;i<size*2;i++){
- if( input[i%size] == input[tarindex])
- return i%size;
- }
- return -1;
- }
- */
- /*
- * === FUNCTION ======================================================================
- * Name: main
- * Description:
- * =====================================================================================
- */
- int
- main ( int argc, char *argv[] )
- {
- int input[] = {0,1,2,3,4,4,5,4,2,3,1,0,2,3,3,1,1,0,3,2,1,4,5,3,4,2,4,1,0,5};
- int n = 6;
- int size = sizeof(input)/sizeof(int);
- int end[size];
- int endnum;
- int i = 0;
- int minlen = size;
- for(i = 0; i<size;i++){
- end[i] = getfirstvalue(i,input,n,size);
-
- endnum=end[i]%size;
- printf ( "start = %d end = %d\n", i,endnum );
- minlen = (end[i]-i+1)>minlen? minlen : end[i]-i+1;
- }
- printf ( "min length = %d\n", minlen );
- return EXIT_SUCCESS;
- } /* ---------- end of function main ---------- */
我的思路呢,就是把每一颗珠子遍历一遍,求出每一个的最小值,然后比较minlen
如果小于minlen就修改minlen的值
求最小值的方法借用了跑黑同学左移填满数据位的方法