留下的學生:
軍訓時有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