• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天之道

            享受編程的樂趣。
            posts - 118, comments - 7, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            編寫一個返回循環(huán)鏈表中節(jié)點數(shù)的函數(shù)

            實現(xiàn)代碼如下:

            #include<stdio.h>
            #include<stdlib.h>
            typedef struct node *link;
            struct node{ int item; link next; };

            int node_number(link p,int n)
            {
                int count=0,i;
                for(i=0;i<n-1;i++)
                {
                    p=p->next;
                }
                while(p->item)
                {
                    p->item=0;
                    p=p->next;
                    count++;
                }
                return count;
            }

            int main()
            {
                
                int i,N;
                link t=(link)malloc(sizeof(node));
                t->item=1;
                t->next=t;
                link x=t;
                for(i=2;i<=10;i++)
                {
                    x = (x->next= (link)malloc(sizeof(node)));
                    x->item=i;
                    x->next=t;
                }
                printf("Please input the order of node: ");
                scanf("%d",&N);
                printf("total number of nodes is: %d\n",node_number(t,N));
                return 0;
            }

            posted @ 2012-08-17 01:43 hoshelly 閱讀(291) | 評論 (0)編輯 收藏

            假設(shè)有N個人決定選出一個領(lǐng)導(dǎo)人,方法如下:所有人排成一個圓圈,按順序數(shù)數(shù),每隔第M個人出局,此時他兩邊的人靠攏重新形成圓圈。問題是找出哪一個人將會是最后剩下的那個人。我們希望打印出所有人的出局順序和最后選出的領(lǐng)導(dǎo)人是哪一位。

            這個問題稱為約瑟夫問題,可以利用鏈表解決。

            代碼如下:

              //約瑟夫問題
              
              #include<stdio.h>
              #include<stdlib.h>
              typedef struct node *link;
              struct node { int item; link next; }; //定義結(jié)點
              int main()
              {
                 int i,N,M;
                 printf("Input N and M: "); //N表示共有N個人,M表示每隔第M個人要出局
                 scanf("%d%d",&N,&M);
                 link t = (link)malloc(sizeof(node)); //新建結(jié)點t
                 link x=t; 
                 t->item = 1; t->next=t; //創(chuàng)建一個代表1號的單個節(jié)點的循環(huán)鏈表
                 for(i=2;i<=N;i++)
                 {
                     x=(x->next= (link)malloc(sizeof(node)));//將2~N號按序插到之前創(chuàng)建的單個節(jié)點的循環(huán)鏈表中
                     x->item=i; x->next=t;
                 }
             
                 while(x!= x->next) //如果不是最后一個節(jié)點,因為是循環(huán)鏈表,所以x!=x->next
                 {
                     for(i=1;i<M;i++) //則順著鏈表向前遍歷,數(shù)出M-1個元素
                         x=x->next;
                     printf("%d ",x->next->item);
                     x->next = x->next->next; //刪除第M個元素
                     N--; //節(jié)點數(shù)減1
                 }
                 printf("\n%d\n",x->item); //最后打印出最后一個節(jié)點
                 return 0;
             }

            posted @ 2012-08-17 00:14 hoshelly 閱讀(1057) | 評論 (0)編輯 收藏

            編寫程序,對于N個隨機(jī)產(chǎn)生的單位正方形中的點,統(tǒng)計可以被長度小于d的直線連結(jié)的點對數(shù)。

            程序如下:

            typedef structfloat x; float y; } point; //定義點的數(shù)據(jù)類型
            float distance(point,point); //兩點之間距離函數(shù)
            float distance(point a,point b)

               float dx= a.x - b.x, dy= a.y - b.y;
               return sqrt(dx*dx + dy*dy);
            }
            //以上為頭文件 Point.h 的內(nèi)容



            #include<stdio.h> 
            #include<stdlib.h>
            #include<math.h>
            #include "Point.h"
            float randFloat()
            {  return 1.0*rand()/RAND_MAX; } //產(chǎn)生隨機(jī)數(shù)的函數(shù)
            int main()
            {
                float d,N;
                int i,j,cnt=0;
                scanf("%f%f",&d,&N); //d為要求兩點之間距離小于的長度,N為測試的點 
                point *a = (point *)malloc(sizeof(point)*N); //動態(tài)生成數(shù)據(jù)類型為point的數(shù)組a
                for(i=0;i<N;i++)
            {
            a[i].x = randFloat(); a[i].y = randFloat(); 
            }
                for(i=0;i<N;i++)
                    for(j=i+1;j<N;j++)
                        if(distance(a[i],a[j])<d) //如果兩點之間的距離小于d,那么cnt加1
                                 cnt++;
                printf("%d edges shorter than %f\n",cnt,d); //輸出有多少條邊的長度小于d
                return 0;
            }

            posted @ 2012-08-14 21:39 hoshelly 閱讀(319) | 評論 (0)編輯 收藏


            //模擬拋硬幣的實驗

            #include<stdio.h>
            #include<stdlib.h>
            int heads()   //返回0或非0值
            {
                return rand() <RAND_MAX/2;
            }

            int main()
            {
                int i,j,cnt;
                int N,M;
                scanf("%d%d",&N,&M); //拋一枚硬幣N=32次,如此做M=1000次這樣的實驗
                int *f=(int *)malloc((N+1)*sizeof(int));
                for(j=1;j<=N;j++) //初始化數(shù)組全部為0值
                    f[j]=0;
                for(i=1;i<M;i++,f[cnt]++)  //開始拋硬幣,f[cnt]記錄第cnt次拋硬幣出現(xiàn)正面的次數(shù)
                    for(cnt=1,j=1;j<=N;j++) //開始第一輪共32次的拋硬幣實驗
                        if(heads())    cnt++; //如果出現(xiàn)正面,即heads()返回值為1,則對應(yīng)著f[cnt]++,同時cnt++,此處利用數(shù)組索引統(tǒng)計正面出現(xiàn)次數(shù),負(fù)面數(shù)組值始終為0
                        
                for(j=1;j<=N;j++)
                {
                    printf("%2d ",j);
                    for(i=0;i<f[j];i+=10) printf("*"); //正面每出現(xiàn)十次打印一個星號
                    printf("\n");
                }
                return 0;
            }

            posted @ 2012-08-14 10:37 hoshelly 閱讀(846) | 評論 (0)編輯 收藏

            // From < C Programming FAQs > 
            找出所有小于10000的素數(shù),算法原理請自行g(shù)oogle 埃拉托色尼篩法

            程序代碼:

            #define N 10000
            #include<stdio.h>
            int main()
            {
                int i,j,a[N];
                for(i=2;i<N;i++) a[i]=1; //將數(shù)組中的值全部設(shè)為1
                for(i=2;i<N;i++)
                    if(a[i])
                        for(j=i;i*j<N;j++)   a[i*j]=0; //將索引為2,3,5,的倍數(shù)的數(shù)組元素設(shè)為0,因為這些數(shù)不是素數(shù)
                for(i=2;i<N;i++)
                    if(a[i]) printf("4%d\n",i); //遍歷打印出找到的素數(shù)
                printf("\n");
                return 0;
            }

            posted @ 2012-08-13 22:17 hoshelly 閱讀(584) | 評論 (0)編輯 收藏

            輸入10個學(xué)生的成績,編寫一程序?qū)W(xué)生的成績按從高到低輸出,要求用鏈表實現(xiàn)。

            #include<stdio.h>
            #include<stdlib.h>
            struct Stu
            {
                int score;
                struct Stu *next;
            };
            typedef struct Stu Node;
            int main()
            {
                int i;
                Node *head,*p,*q;
                head=(Node*)malloc(sizeof(Node)); //創(chuàng)建頭結(jié)點
                if(head == NULL)
                {
                    printf("Memory is not enough!");
                    return 0;
                }
                head->next=NULL;
                for(i=0;i<10;i++)
                {
                    p=(Node*)malloc(sizeof(Node)); //創(chuàng)建一個新結(jié)點p
                    if(p == NULL)
                    {
                        printf("no enough memory!");
                        return 0;
                    }
                    printf("Input the %dth student's score: ",i+1);
                    scanf("%d",&p->score); //輸入成績
                    q=head;
                    while(q->next != NULL) //遍歷鏈表
                    {
                        if(q->next->score < p->score) //如果發(fā)現(xiàn)鏈表中的某個成績比當(dāng)前輸入成績小,就跳出循環(huán),在其前面插入當(dāng)前輸入成績
                            break;
                        q=q->next; //繼續(xù)遍歷直到遍歷的成績比當(dāng)前輸入的成績小
                    }
                    p->next=q->next; //這是當(dāng)前成績插入到鏈表中比其小的成績前面的代碼
                    q->next=p;

                }
                p=head->next;
                while(p !=NULL)  
                {
                    printf("%d ",p->score);
                    p=p->next;
                }

            p=head;
            while(p->next !=NULL)
            {
                q=p->next;
                p->next=q->next;
                free(q);
            }
            free(head);

            return 0;
            }

            posted @ 2012-08-12 22:23 hoshelly 閱讀(2030) | 評論 (0)編輯 收藏

            編寫一個函數(shù)totsubstrnum(char *str, char *substr) ,它的功能是:統(tǒng)計子字符串substr在字符串str中出現(xiàn)的次數(shù)。

            思想:len2為子串的長度,設(shè)置變量 i =0, 利用strncmp函數(shù)將str+i 開始的len2個字符與子串substr進(jìn)行比較,如果相等,則count加1,此時 i 加 len2,如果不等,則 i 加1,繼續(xù)尋找。

            代碼測試通過:

            #include<stdio.h>
            #include<string.h>
            int totsubstrnum(char *str, char *substr);
            int main()
            {
                char str[80],substr[80];
                printf("Input string: ");
                gets(str);
                printf("Input substring: ");
                gets(substr);
                printf("count = %d\n",totsubstrnum(str,substr));

                return 0;
            }

            int totsubstrnum(char *str, char *substr)
            {
                int i=0,count=0,len1,len2;
                len1=strlen(str);
                len2=strlen(substr);
                while(i <= len1-len2)
                {
                    if(strncmp(str+i,substr,len2) == 0)
                    {
                        count++;
                        i +=len2;
                    }
                    else
                        i++;
                }
                return (count);
            }

            posted @ 2012-08-12 16:05 hoshelly 閱讀(1644) | 評論 (0)編輯 收藏

            編寫一函數(shù)strlshif(char *s, int n),其功能是把字符串s中的所有字符左移n個位置,字符串中的前n個字符移到最后。

            代碼測試通過:


            #include<stdio.h>
            #include<string.h>
            void strlshif(char *s, int n);
            void main()
            {
                char str[]="0123456789";
                strlshif(str,3);
                printf("%s\n",str);
            }

            void strlshif(char *s, int n)
            {
                int i,len;
                char ch;
                len=strlen(s);
                for(i=0;i<n;i++)
                {
                    ch=s[0];
                    strncpy(s,s+1,len-1);
                    s[len-1]=ch;
                }
            }

            那么若是不用strncpy函數(shù)功能,如何使指定的字符串左移n位?
            代碼測試通過,如下:

            #include<stdio.h>
            #include<string.h>
            int main()
            {
                char str[]="0123456789";
                char sstr[80]={0}; //使用一數(shù)組儲存移動后的字符串
                char *p;
                int c,j;
                static int i,n;
                p=&str[0];
                printf("input the number: \n");
                scanf("%d",&n); //輸入要左移的前n個字符,即將這n個字符移動到最后面
                c=n;
                while( c-- && p++ ); //找到?jīng)]有移動過的剩下的全部字符,把它們儲存在數(shù)組sstr 中

                    for(i=0;i<strlen(str)-n;i++)
                    {
                        sstr[i]= *p;
                        p++;
                    }
                    p=&str[0];  //指針指向第一個字符
                    for(j=i;j<strlen(str);j++) //將要移動的字符一個一個地“接”到數(shù)組sstr后面
                    {
                        sstr[j]= *p;
                        p++;
                    }
                    sstr[j]='\0'; //最后字符串結(jié)尾用'\0'
                    printf("%s",sstr);  
                    return 0;
            }



                    

            posted @ 2012-08-12 14:14 hoshelly 閱讀(927) | 評論 (0)編輯 收藏

            // s 是字符串 startloc 是開始取的位置   len表示取得子串長度
            #include<stdio.h>
            #include<string.h>
            #include<stdlib.h>
            void substr(char *s, int startloc,int len)
            {
                if((startloc < 0) || (startloc >= strlen(s)) || (len<0))
                {
                    printf("input error!");
                    exit(0);
                }
                int i,c=0;
                char sstr[80];
                while(*s !='\0')
                {
                    if(c!=startloc)
                    {
                        ++c;
                        s++;
                    }
                    else
                    {
                        for(i=0;i<len;i++)
                        {
                            sstr[i]= *s;
                            s++;
                        }
                        sstr[i]='\0';
                        break;
                    }
                }
                printf("%s",sstr);
            }

            int main()
            {
                char str[80];
                int s,l;
                printf("Input string: ");
                gets(str);
                printf("Start Location: ");
                scanf("%d",&s);
                printf("Substring length: ");
                scanf("%d",&l);
                substr(str,s,l);
                return 0;
            }

                

            posted @ 2012-08-12 11:45 hoshelly 閱讀(238) | 評論 (0)編輯 收藏

            類似C語言中的strcat()函數(shù),編程實現(xiàn)mystrcat( char *str, char * destr)的功能并測試 。


            代碼測試通過:

            #include<stdio.h>
            void mystrcat( char *str,char *destr)
            {
                while(*str !='\0')
                    str++;
                while(*destr !='\0')
                {
                    *str = *destr;
                    str++;
                    destr++;
                }

                *str = '\0';
            }

            int main()
            {
                char str[30],destr[30];
                printf("input string and substring: ");
                scanf("%s%s",str,destr);
                mystrcat(str,destr);
                printf("%s",str);
                return 0;
            }

            posted @ 2012-08-12 10:16 hoshelly 閱讀(343) | 評論 (0)編輯 收藏

            僅列出標(biāo)題
            共12頁: First 2 3 4 5 6 7 8 9 10 Last 
            国内精品久久久久久麻豆| 国产A三级久久精品| 亚洲国产成人久久综合一| 久久996热精品xxxx| 久久久久青草线蕉综合超碰| 新狼窝色AV性久久久久久| 99久久精品免费看国产一区二区三区| 久久国产视屏| 久久精品中文字幕无码绿巨人| 蜜桃麻豆www久久| 国产毛片欧美毛片久久久 | 久久亚洲电影| 国产精品一区二区久久不卡| 日本久久中文字幕| 99久久精品久久久久久清纯| 色婷婷综合久久久中文字幕| 亚洲午夜精品久久久久久浪潮 | 日韩乱码人妻无码中文字幕久久| 国产精自产拍久久久久久蜜 | 久久天堂电影网| 亚洲精品乱码久久久久久蜜桃不卡| 99久久免费只有精品国产| 激情伊人五月天久久综合| 国产精品久久久久a影院| 久久久久亚洲精品男人的天堂| www.久久热.com| 久久婷婷五月综合国产尤物app| 欧美激情一区二区久久久| 久久影院午夜理论片无码| 精品一久久香蕉国产线看播放| 久久99国产精品久久久 | 久久亚洲国产精品五月天婷| 国产精品久久久久久影院| 久久久久99精品成人片试看| 精品久久久无码21p发布| 色综合久久夜色精品国产| 久久久午夜精品福利内容| 精品综合久久久久久97| 亚洲愉拍99热成人精品热久久 | 国内精品人妻无码久久久影院 | 久久国产视屏|