hdu 1443 Joseph
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                hdu 1443 Joseph
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1443
先把神級代碼雙手奉上....雖然看不懂吧...囧
1 #include<stdio.h> 2 3 int ans[14]={0}; 4 5 int joseph(int k) 6 { 7 int cnt,p; 8 if(ans[k])return ans[k]; 9 for(int i=k+1;;i++) 10 { 11 for(cnt=k<<1,p=0;cnt>k;cnt--) 12 { 13 p=(p+i-1)%cnt; 14 if(p<k)cnt=0; 15 } 16 if(cnt==k) 17 { 18 ans[k]=i; 19 return i; 20 } 21 } 22 return 0; 23 } 24 25 int main() 26 { 27 int n; 28 while(scanf("%d",&n),n) 29 { 30 printf("%d\n",joseph(n)); 31 } 32 return 0; 33 }
再把神奇代碼奉上...打表
?
1 #include<stdio.h> 2 3 int main(){ 4 int n,a[14]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881}; 5 while(scanf("%d",&n)&&n) 6 printf("%d\n",a[n]); 7 return 0; 8 }?
再說這幾個(gè)數(shù)是怎么來的.....
#include<iostream> using namespace std; typedef struct joseph { int next; int pre; int cur; }joseph; int main() {int k,m,count,total,i,j,rec1,rec2,a[14];joseph p[30];count=0;for(j=1;j<=13;j++){for(m=2;count!=j*2;m++){for(i=0;i<2*j;i++){p[i].cur=i;p[i].next=i+1;p[i].pre=i-1;}p[2*j-1].next=p[0].cur;p[0].pre=p[2*j-1].cur; //將每個(gè)人連成環(huán)count=0;rec1=0;total=2*j;do{for(i=1;i<=(m-1)%total;i++){rec1=p[rec1].next;} //一個(gè)一個(gè)往后移動(dòng),直到不能再移動(dòng)了 rec2=p[rec1].next;p[p[rec1].pre].next=rec2;p[rec2].pre=p[rec1].pre; //刪除需要出圈者if(p[rec1].cur>=0 && p[rec1].cur<j) //出圈了好人,跳出該m值的循環(huán) {break;}else{rec1=rec2;count++; //出圈一個(gè)壞人計(jì)數(shù)器加1 }total--; //出圈一人后總?cè)藬?shù)記得減一}while(count!=j);if(count==j) //出圈好人前出圈完K個(gè)壞人,滿足條件 {a[j]=m;break;}}}while(cin>>k && k){cout<<a[k]<<endl;}return 0; }
這代碼竟然沒超時(shí)T_T.....
轉(zhuǎn)載于:https://www.cnblogs.com/xurenwen/p/3875022.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的hdu 1443 Joseph的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: CodeForces 451A
- 下一篇: cocos2d_android 瞬间动
