围成一圈报数(123...报数)

 

asp:


< %

' 围成一圈报数(123报数)

Dim  sAction
sAction 
=   Trim (Request.QueryString( " action " ))
If  sAction  =   " do "   Then
    
Dim  num, spe  ' 人数,报数间隔
    num  =   Trim (Request.Form( " num " ))
    spe 
=   Trim (Request.Form( " spe " ))
    
    
' 错误处理
     If   IsNumeric (num)  =   False   Or   IsNumeric (spe)  =   False   Then
        OutputError 
" 人数和报数间隔只能是数字 "
    
End   If
    
If  num  <  spe  Then
        OutputError 
" 人数不能小于报数间隔 "
    
End   If
    
    
Dim  aNum
    
Dim  i, j
    
ReDim  aNum(num)
    
For  i  =   1   To  num
        aNum(i) 
=  i  ' 号数和数组下标一一对应
     Next

    
Dim  n_leaving, n_Pos, t  ' 剩余人数, 当前报数人的位置
    n_leaving  =  num  ' 初始化剩余人数
    n_Pos  =   0   ' 初始化开始报数位置,0表示从1号开始报数,1表示从2号开始报数
     For  i  =   1   To  n_leaving
        
If  n_leaving  =   1   Then   Exit   For   ' 如果最后只剩下一个人,报数结束

        n_Pos 
=  (n_Pos  +  spe)  Mod  n_leaving  ' 算出下次报数的人位置
         If  n_Pos  =   0   Then
            n_Pos 
=  n_leaving    
        
End   If
        
        
' 将已经报数的人移到数组的尾部
        t  =  aNum(n_Pos)
        
For  j  =  n_Pos  To  n_leaving - 1
            aNum(j) 
=  aNum(j + 1 )    
        
Next
        aNum(n_leaving) 
=  t

        n_Pos 
=  n_Pos  -   1   ' 移走一个人后,需退一个位置
        n_leaving  =  n_leaving  -   1   ' 留下继续报数的人数减1
     Next


    
' 输出结果
    Response.Write  " <b>人数:</b> "   &  num  &   " <br/> "
    Response.Write 
" <b>报数间隔:</b> "   &  spe  &   " <br/> "
    Response.Write 
" <b>结果:</b> "
    
Dim  s
    
For  i = num  To   1  Step  - 1
        s 
=  s  &  aNum(i)  &   " , "
    
Next
    s 
=   Left (s,  Len (s) - 1 )
    Response.Write s

End   If


Sub  OutputError(str)
    Response.Write 
" <script language='javascript'>alert(' "   &  str  &   " ');history.back();</script> "
    Response.End
End Sub

%
>
< html >
< head >
< title > 围成一圈报数 </ title >
</ head >

< body >
< p >& nbsp; </ p >
< p >& nbsp; </ p >
< p align = " center " >
< table border = 0 >
< form method = " post "  action = " ?action=do " >
    
< tr >< td > 总人数: </ td >< td >< input type = " text "  name = " num "  value = "" ></ td ></ tr >
    
< tr >< td > 报数间隔: </ td >< td >< input type = " text "  name = " spe "  value = "" ></ td ></ tr >
    
< tr >< td colspan = " 2 " >< input type = " submit "  value = " 查看结果 " ></ td ></ tr >
</ form >
</ table >
</ p >
</ body >
</ html >






< %

' 围成一圈报数(123报数)

Dim  sAction
sAction 
=   Trim (Request.QueryString( " action " ))
If  sAction  =   " do "   Then
    
Dim  num, spe  ' 人数,报数间隔
    num  =   Trim (Request.Form( " num " ))
    spe 
=   Trim (Request.Form( " spe " ))
    
    
' 错误处理
     If   IsNumeric (num)  =   False   Or   IsNumeric (spe)  =   False   Then
        OutputError 
" 人数和报数间隔只能是数字 "
    
End   If
    
If  num  <  spe  Then
        OutputError 
" 人数不能小于报数间隔 "
    
End   If
    
    
Dim  aNum
    
Dim  i
    
ReDim  aNum(num)
    
For  i  =   1   To  num
        aNum(i) 
=   0   ' 初始化数组元素为0,表示全部未报数过;如果报数过,数组元素值为 报数顺序
     Next


    
Dim  n_Order, n_Count  ' 报数顺序, 计数
    n_Order  =   0
    n_Count 
=   0
    
    
    
' 输出结果
    Response.Write  " <b>人数:</b> "   &  num  &   " <br/> "
    Response.Write 
" <b>报数间隔:</b> "   &  spe  &   " <br/> "
    Response.Write 
" <b>结果:</b> "

    
Dim  s
    
Do   While   True
        
If  IsDone(aNum)  =   True   Then   Exit   Do   ' 如果已经全部报数,那么退出

        
For  i  =   1   To  num
            
If  aNum(i)  =   0   Then
                n_Count 
=  n_Count  +   1
                
If  n_Count  Mod  spe  =   0   Then
                    n_Order 
=  n_Order  +   1
                    aNum(i) 
=  n_Order
                    s 
=  s  &  i  &   " , "
                
End   If
            
End   If
        
Next

    
Loop
    
    s 
=   Left (s,  Len (s) - 1 )
    Response.Write s

End   If


Sub  OutputError(str)
    Response.Write 
" <script language='javascript'>alert(' "   &  str  &   " ');history.back();</script> "
    Response.End
End Sub

Function  IsDone(a_Num)
    
Dim  i
    
For  i  =   1   To   UBound (a_Num)
        
If  a_Num(i)  =   0   Then
            IsDone 
=   False
            
Exit   Function
        
End   If
    
Next
    IsDone 
=   True
End Function


%
>
< html >
< head >
< title > 围成一圈报数 </ title >
</ head >

< body >
< p >& nbsp; </ p >
< p >& nbsp; </ p >
< p align = " center " >
< table border = 0 >
< form method = " post "  action = " ?action=do " >
    
< tr >< td > 总人数: </ td >< td >< input type = " text "  name = " num "  value = "" ></ td ></ tr >
    
< tr >< td > 报数间隔: </ td >< td >< input type = " text "  name = " spe "  value = "" ></ td ></ tr >
    
< tr >< td colspan = " 2 " >< input type = " submit "  value = " 查看结果 " ></ td ></ tr >
</ form >
</ table >
</ p >
</ body >
</ html >

c语言:
/*
【程序69】
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出
   圈子,问最后留下的是原来第几号的那位。
*/

#include 
" stdio.h "
#include 
" conio.h "

#define  TOTAL 5
#define  BEGIN 2
#define  SPE 3
main()
{
    
int person[TOTAL];
    
int i,j;
    
int *p;
    
int leave,begin,cur,next,t;
    p
=person;
    begin
=BEGIN-1;

    
for(i=0;i<TOTAL;i++)
    
{
      
*(p+i)=i+1;
      printf(
"%d",*(p+i));
    }

    printf(
"\n");

    leave
=TOTAL;
    cur
=begin;
    
for(i=begin;i<leave;i++)
    
{
       next
=(cur+SPE-1)%leave;

       printf(
"\n%d,%d\n",next,leave);

       t
=*(p+next);
       
for(j=next;j<leave-1;j++)
       
{
          
*(p+j)=*(p+j+1);
       }

       
*(p+leave-1)=t;
       leave
--;
       cur
=next;

       i
=0;
    }


    
for(i=TOTAL-1;i>=0;i--)
    
{
       printf(
"%d",*(p+i));
    }

    
    getch();
}

你可能感兴趣的:(围成一圈报数(123...报数))