zoukankan      html  css  js  c++  java
  • hdu 1443 Joseph

    题目链接: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
    ",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
    ",a[n]);
    7  return 0;
    8 }

    再说这几个数是怎么来的.....

    #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;                                  //将每个人连成环
           count=0;
           rec1=0;
           total=2*j;
           do
           {
               for(i=1;i<=(m-1)%total;i++)
                  {rec1=p[rec1].next;}     //一个一个往后移动,直到不能再移动了 
               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值的循环
               {
                   break;
               }
               else
               {
                   rec1=rec2;
                   count++;                      //出圈一个坏人计数器加1 
               }
               total--;                                   //出圈一人后总人数记得减一
           }while(count!=j);
           if(count==j)                               //出圈好人前出圈完K个坏人,满足条件
           {
               a[j]=m;
               break;
           }
       }
     }
    
     while(cin>>k && k)
     {
        cout<<a[k]<<endl;
     }
     return 0;
    }


    这代码竟然没超时T_T.....

  • 相关阅读:
    Struts2---配置文件讲解及简单登录示例
    Struts2---环境搭建及包介绍
    Struts2---概述
    Spring---bean的作用域
    Spring---bean的实例化
    Sql Server 2005 .bak备份文进行还原数据库
    Windows Server 2003 R2 With Sp2 序列号
    [Idea]安装avtiviti插件以及 插件中文乱码
    IDEA 跑spring项目找不到get,set的问题
    SpringBoot 集成Avtiviti步骤
  • 原文地址:https://www.cnblogs.com/xurenwen/p/3875022.html
Copyright © 2011-2022 走看看