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,每次杀人前都要检查一遍是否还要继续杀人了,因为要留下最后一个人。

  • 相关阅读:
    vue中用解构赋值的方法引入组件
    es6--promise
    VUE设置浏览器icon图标
    vue项目之购物车
    vue之组件通信
    hbulider 快捷键
    Redis详解(3)--5大数据类型
    Redis详解(1)--redis简介与安装
    Redis详解(2)--redis配置文件介绍
    Python面试综合--web相关
  • 原文地址:https://www.cnblogs.com/coversky/p/7294149.html
Copyright © 2011-2022 走看看