分頁導航演算法

來源:互聯網
上載者:User

php寫起來不太好,演算法還是用C或C++寫起來方便些:

#include <stdio.h>#include <stdlib.h>void out_put(int start,int end,int cur){int i = start;for(;i<=end;i++){if(i==cur) printf("* ");else printf("%d ",i);}}void navi(int cur,int page){    int show=9, hf=show/2;    if(cur!=1) printf("Prev ");    if(page<=show){out_put(1,page,cur);    } else {        if(cur-1<hf+2){out_put(1,cur,cur);        } else {            printf("1 ");            if(page!=show+1) printf("... ");out_put(cur-hf+(page-cur-hf>0?0:page-cur-hf),cur,cur);        }        if(page-cur<hf+2){out_put(cur+1,page,cur);        } else {out_put(cur+1,cur+hf-(cur-hf>1?0:cur-hf-1),cur);            if(page!=show+1) printf("... ");            printf("%d ",page);        }    }if(cur!=page) printf("Next");}int main(int argc,char** argv){    if(argc!=3){        printf("Parameters error!\n");        return -1;    }    navi( atoi(argv[1]), atoi(argv[2]) );printf("\n");    return 0;}

 

編譯運行看一下:

gpp命令實際上就是gcc命令,會編譯出來一個a.exe,用a就可以運行,接受兩個參數,第一個是當前頁,第二個是總頁數。

還有些地方需要改進,歡迎大家提意見。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.