日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C之约瑟夫问题

發布時間:2025/3/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C之约瑟夫问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?/* 15個人排成一圈,給他們1——15的編號 數到4的人退出,余下的人從下一個位置重新開始報數,直到最后只剩下一個人

問 這個人是幾號? */ ==================數組實現==========================
  • #include?<iostream>?
  • #include?<stdio.h>?
  • using?namespace?std;?
  • ?
  • int?list[15];?
  • ?
  • void?print()?
  • {?
  • ????cout<<"----------剩下的元素為-------------"<<endl;?
  • for?(int?i=0;i<15;i++)?
  • {?
  • ????cout<<list[i]<<endl;?
  • }?
  • }?
  • void?init()?
  • {?
  • ????for?(int?i=0;i<15;i++)?
  • ????{?
  • ????????list[i]=i+1;?
  • ????}?
  • ?
  • ?????
  • ?????????
  • }?
  • void?setposition(int?*&p)??//這個函數是最難的地方?
  • {?
  • ????int?sum=0;?
  • ????int?a=3;?
  • ????while(sum!=a)?
  • ????{????
  • ?????
  • ????????if?(!*p)?
  • ????????{?//p為0的時候??也?就是說傳遞進來的是0,所以a=4;?
  • ????????????a=4;?
  • ????????????while?(!*p)??????//找到下一個不為0的點。?
  • ????????????{?
  • ????????????????p++;?
  • ????????????????if?(p==list+15)?
  • ????????????????{?
  • ????????????????????p=list;??
  • ????????????????}?
  • ?????????????????
  • ????????????}?
  • ????????}?
  • ????else?
  • ????{??//p不為0的時候??
  • ?????
  • ????????p++;?
  • ????????if?(p==list+15)???//注意檢驗p是否為最后一個元素。?
  • ????????{?
  • ????????????p=list;??
  • ????????}?
  • ????????if?(!*p)?
  • ????????{??
  • ?????????????
  • ????????????while?(!*p)?
  • ????????????{?
  • ????????????????p++;?
  • ????????????????if?(p==list+15)?
  • ????????????????{?
  • ????????????????????p=list;???
  • ????????????????}?
  • ?????????????????
  • ????????????}?
  • ????????}?
  • ?
  • ????}?
  • ?
  • ????sum++;?
  • ????}?
  • }?
  • int?main()?
  • {?
  • init();?
  • int?*p=list;?
  • for?(int?count=0;count<14;count++)?
  • {?
  • ?????
  • setposition(p);?
  • *p=0;?
  • ?
  • print();?
  • }????
  • return?0;?
  • }?
  • =================鏈表實現===========================
  • #include?<iostream>?
  • #include?<stdio.h>?
  • using?namespace?std;?
  • ?
  • struct?list??
  • {?
  • ????int?data;?
  • ????list?*?next;?
  • };?
  • ?
  • ?list?*head=new?list;?
  • ?
  • ?list?*tail=head;?
  • ?
  • ?void?add()?
  • ?{?
  • ????????head->data=1;?
  • ?????for?(int?i=2;i<16;i++)?
  • ?????{?
  • ?????????list?*p=new?list;//記得清零?
  • ?????????memset(p,0,sizeof(p));?
  • ?????????tail->next=p;?
  • ?????????p->data=i;?
  • ?????????p->next=head;?
  • ?????????tail=p;?
  • ?????}?
  • ?
  • ?}?
  • ?
  • void?out()?
  • {?
  • ????cout<<endl<<endl<<endl;?
  • ????cout<<"剩下的人:"<<endl;?
  • ????list?*p=head;?
  • //??printf("head:%d\ttail:%d\t\n",head,tail);?
  • ????while(p!=tail)?
  • ????{?
  • ????????cout<<p->data<<endl;?
  • ????????p=p->next;?
  • ?
  • ????}?
  • //??????cout<<p->data<<endl;?
  • //??????cout<<"out?over"<<endl;?
  • //??????????int?_a_;cin>>_a_;?
  • }?
  • void?remove()?
  • {?
  • ????list?*p=head;?
  • ????list?*lastp=p;?
  • ?
  • ????for(int?count=0;count<14;count++)?
  • ????{????
  • ????????????????out();?
  • ????????????p=p->next;?
  • ????????????for?(int?i=0;i<2;i++)?
  • ????????????{????
  • ????????????????lastp=lastp->next;?
  • ????????????????p=p->next;?
  • ????????????}?
  • ?
  • ????//??cout<<"free?"<<p->data<<endl;?
  • ????????lastp->next=p->next;?
  • ????????if?(p==head)??//here;?
  • ????????{?
  • ????????????head=lastp->next;?
  • ????????}?
  • ????????if?(p==tail)?
  • ????????{?
  • ????????????tail=lastp;?
  • ????????}?
  • ????????free(p);?
  • ????????p=lastp->next;?
  • ????????lastp=p;?
  • ?????????
  • ????}?
  • }?
  • ?
  • int?main()?
  • {?
  • add();?
  • remove();?
  • return?0;?
  • }?
  • ========================================= 感想:本來用鏈表完了以后覺得挺麻煩的,這問題用數組解決應該很容易,一實踐才發現根本不是那回事,數組需要考慮的非常周密,編寫的過程中犯了很多錯誤,只好一點一點修改解決。 現在快累死了,從7點到十點。解決了這個問題,總的來說 還是有一點收獲的。 2011年12月26日 21:57:43

    ?

    轉載于:https://blog.51cto.com/awsam/751675

    總結

    以上是生活随笔為你收集整理的C之约瑟夫问题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。