菜园四周种了n棵白菜,并按顺时针方向有1到n编号。收割时,从编号开始,按顺时针方向每隔两颗白菜收割一棵,直到全部收割完毕。按收割顺序列出白菜的编号。
// vegetable.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"iomanip.h"
#include "iostream.h"
#define MAXSIZE 100
void reap(int B[],int n)
{
int i,j,k,s,t; //s为每一次循环的次数,
int *A = new int [n];
j = 0;
k = 3;
s = n;
for(i = 0;i < n;i ++)
A[i] = i + 1;
while(j < n)
{
t = s;
s = 0;
for(i = 0; i < t;i++)
{
if(--k != 0)
A[s++]=A[i]; //未收割的白菜
else
{
B[j++] = A[i]; //收割的白菜
k = 3;
}
}
}
}
int main(int argc, char* argv[])
{
int a[MAXSIZE]={0};
int n,i ;
cout<<"please input the numbers of array"<<endl;
cin>>n;
reap(a,n);
for(i = 0;i<n;i++)
cout<<a[i]<<setw(10);
return 0;
}
运行结果: