程序设计大赛-留下的学生

来源:互联网 发布:台球直播软件 编辑:程序博客网 时间:2024/05/01 06:28

 留下的学生:

       军训时有N个学生站成一行,从右到做,从1到N依次编号,他们还得到一个整数M.然后这些学生西欧那个右手边的学生开始报数。报的数为M的倍数的学生留在队列里,其他的学生需要离开队列。他们重复进行这项操作直到队列中的人数小于M.

输入格式:

输入包含几组测试数据。每组测试数据只占单独的一行,包含两个整数n和m(3<=n<=10^9,2<=m<=n).当n=0并且m=0的时候结束输入。

输出格式:

对于每组测试数据,输出两列。每一行包含一个整数X,表示最终留下的学生的数目。第二行包含X个整数,表示最终留下的学生的编号,各个整数之间用1个空格隔开。如:

输入:

10 3

8 3

0 0

输出:

1

9

2

3 6

我的程序:

#include<iostream.h>
#include<stdio.h>
//求m的n次方
long power(int m,int n)
{
    long sum=1;
    for(int i=0;i<n;i++)
        sum*=m;
    return sum;
}
//清空文件
void cleanfile()
{
    FILE *pt;
    if(NULL==(pt=fopen("output.txt","w")))
    {
        cout<<"can't open the file!";
    }
    else
    {
        fclose(pt);
    }
   
}
void main()
{
    long n,m,k,num;
    FILE *pt1,*pt2;
    int a[100000],i;
    cleanfile();
    if(NULL==(pt1=fopen("input.txt","r")))
    {
        cout<<"can't open the file!";
    }
    else
    {
        fscanf(pt1,"%d",&n);
        fscanf(pt1,"%d",&m);
       
        while(!(n==0 && m==0))
        {
            k=n;
            num=0;
            while(k>=m)
            {
                k=k/m;
                num++;
            }
           
            for(i=0;i<k;i++)
            {
                a[i]=power(m,num)*(i+1);
            }
           
            fscanf(pt1,"%d",&n);
            fscanf(pt1,"%d",&m);
            if(NULL==(pt2=fopen("output.txt","a")))
            {
                cout<<"can't open the file!";
            }
            else
            {
                fprintf(pt2,"%d/n",k);
                if(n==0 && m==0)
                {
                    for(i=0;i<k-1;i++)
                    {
                        fprintf(pt2,"%d ",a[i]);
                    }
                    fprintf(pt2,"%d",a[k-1]);
                }
                else
                {
                    for(i=0;i<k-1;i++)
                    {
                        fprintf(pt2,"%d ",a[i]);
                    }
                    fprintf(pt2,"%d/n",a[k-1]);
                }
                fclose(pt2);
            }
        }
        fclose(pt1);
    }
}

输入:

10 3
100 6
8 3
100000000 9
0 0

输出:

1
9
2
36 72
2
3 6
2
43046721 86093442

 

原创粉丝点击