#include<iostream.h>
void main()
{
int interval,i,arraysize,q,k=1;
int *a;
loop:
cout<<"input the arraysize:";
cin>>arraysize;
cout<<"input the interval:";
cin>>interval;
if((a=new int[arraysize])==NULL)
{
cout<<"can't alloate more memory,terminating.\n";
return;
}
q=((interval>1)&&(interval<arraysize)&&(arraysize>=1));
if(q==0)
{
cout<<"输入有误!请选择 1:停止运行 2:重输 "<<endl;
if(cin.get()=='1')
return;
else
if(cin.get()=='2')
goto loop;
}
for(i=0;i<arraysize;i++)
a[i]=i+1;
for(i=0;i<arraysize;i++)
cout<<a[i]<<",";
cout<<endl;
i=-1;
while(1)
{
for(int j=0;j<interval;)
{
i=(i+1)%arraysize;
if(a[i]!=0)
j++;
}
if(k==arraysize)break;
cout<<a[i]<<",";
a[i]=0;
k++;
}
cout<<"\nNo."<<a[i]<<" boy've won.\n";
delete[]a;
}
蓝色部分判断输入是否合法,不合法的话选择1和2.在此,想问下,红色return;为何跳不出函数?
还有就是绿色部分return;,本应该为exit(1);但是编译通不过,改为return;可以通过,它们意思是一样的吧??也是跳出函数.