zoukankan      html  css  js  c++  java
  • 武汉科技大学ACM :1009: 华科版C语言程序设计教程(第二版)习题6.11

    Problem Description

    n个人围成一圈,依次从1至n编号。从编号为1的人开始1至k报数,凡报数为k的人退出圈子,输出最后留下的一个人原来的编号。

    Input

     首先输入一个t,表示有t组数据(1<= t <= 10010)

    然后有t行,每行有2个正整数n和k。(1<= n,k<= 20)

    Output

     对于每组测试数据,输出一个数,表示最后留下来的人的编号。

    Sample Input

    3
    10 3
    7 1
    5 4
    

    Sample Output

    4
    7
    1
    

    HINT

     例如第三组样例:5个人围成一圈,编号1-5。第一轮报数4号出列,第二轮从5开始报数1,3报4,3出列,第三轮从5开始报1,5报4,5出列,第四轮1开始报1,2报4,2出列,最后剩下的为1号。

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int i,j,m,out,n,num[100],*p;
     5     int count;
     6     while(scanf("%d",&count)!=EOF)
     7     {
     8         for(int k=0;k<count;k++)
     9         {
    10             scanf("%d%d",&n,&m);
    11             p=num;
    12             for(i=0;i<n;i++)
    13                 *(p+i)=i+1;
    14             i=0;
    15             j=0;
    16             out=0;
    17             while(out<n-1)
    18             {
    19                 if(*(p+i)!=0)
    20                     j++;
    21                 if(j==m)
    22                 {            
    23                     
    24                     *(p+i)=0;
    25                     j=0;
    26                     out++;            
    27                 }
    28                 i++;
    29                 if(i==n)
    30                     i=0;
    31             }
    32             while(*p==0)
    33                 p++;
    34             printf("%d
    ",*p);
    35             
    36         }    
    37         
    38     }
    39     
    40     return 1;
    41 }
  • 相关阅读:
    iOS8之后,UITableViewRowAction实现滑动多个按钮
    关于UINavigationController的一些技巧
    NSRegularExpression 使用
    UIWindow
    SVN:The working copy is locked due to a previous error (二)
    iOS监听电话来电、挂断、拨号等
    UIDeviceOrientation 和 UIInterfaceOrientation
    java_day03
    java_day 02
    java_day_02
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4159549.html
Copyright © 2011-2022 走看看