zoukankan      html  css  js  c++  java
  • 约瑟夫环解决方案

     1 #include<stdio.h>
     2 /*
     3 *  your own thougt is always best!!
     4 *
     5 */
     6 int couldkill(int *array);//return the array alive whitch is still alive sum;
     7 void kill(int *array);//kill the choose one 
     8 int findalive(int *p);//fand the last one who is still alive.
     9 
    10 int main()
    11 
    12 {
    13     int a[33];int i=0,j=0,count=0;
    14     for(;i<33;i++)
    15     {
    16         a[i]=1;
    17     }
    18     
    19     kill(a);
    20     
    21     printf("%d",findalive(a));        
    22     
    23     
    24 }
    25 int couldkill(int *array)
    26 {    int i=0;int alive=0;
    27 
    28     for(;i<33;i++)
    29     {
    30         if(array[i]==1)
    31         {
    32             alive++;
    33         }
    34     
    35     }
    36     return alive;
    37 }
    38 
    39 void kill(int *array)
    40 {
    41     int i=0,j=0,count=0;
    42     for(;i<33 && couldkill(array)!=1;i++)
    43     {    
    44         if(array[i]!=0)
    45         {
    46             count++;
    47         }
    48 
    49         if(count==7)
    50         {    
    51             count=0;
    52             array[i]=0;
    53             printf("%d is killed
    ",i+1);
    54         }
    55         if(i==32)i=-1;
    56 
    57     }    
    58 
    59 }
    60 int findalive(int *p)
    61 {    int index,i;
    62     for(i=0;i<33;i++)
    63     {
    64         if(p[i]==1)
    65         {
    66             index=i+1;
    67             return index;
    68         }
    69     }
    70 
    71 }

    稍微的说明一下,在这个初始化的33个数组中,1 代表活着,0代表死亡,每次到7的时候,就把该元素的值置为0,每次杀人前都要检查一遍是否还要继续杀人了,因为要留下最后一个人。

  • 相关阅读:
    第二次结对编程作业
    团队项目-需求分析报告
    团队项目-选题报告
    Git安装
    VI编辑,backspace无法删除解决方法
    VM虚拟机,Linux系统安装tools过程遇到 what is the location of the “ifconfig” program
    Ubuntu安装mysql
    Linux配置Tomcat
    Linux配置JDK
    鸡汤
  • 原文地址:https://www.cnblogs.com/coversky/p/7294149.html
Copyright © 2011-2022 走看看