C++程序求解释,小弟刚刚开始写C++程序。
25个人(已编号)围成一圈,123...一顺报数,报的数是3或3的倍数的出去,求最后留下的人的号码(C++程序)
#include<iostream>
#include<math.h>
using namespace std;
void seek(int *a,int *p);
void main (){ int a[25]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
int m;
seek(a,&m);
cout<<"留在圈子中最后的人的序号是"<<m<<endl;}
void seek(int *a,int *m)
{int j=25,k=1;
while(j>1)
{ for(int l=0;l<24;l++){
if(a[l]==0) ;
else if(k%3==0) {
a[l]=0;
--j;
k++;}
else k++;
} }
for(int h=0;h<25;h++){
if(a[h]!=0){
*m=a[h]; }}}
各位看看哪错了,我刚刚开始写C++的程序,但这个就是跑不出来正确的结果,求指导。 c++
[解决办法]
#include<iostream>
#include<math.h>
using namespace std;
void seek(int *a,int *p);
int main ()
{
int a[25]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
int m;
seek(a,&m);
cout<<"留在圈子中最后的人的序号是"<<m<<endl;
}
void seek(int *a,int *m)
{
int j=25,k=1;
while(j>1)
{
for(int l=0;l<24;l++)
{
if(a[l]==0) ;
else if(k%3==0)
{
a[l]=0;
--j;
k++;
}
else k++;
}
}
for(int h=0;h<25;h++)
{
if(a[h]!=0)
{
*m=a[h];
return;
}
}
}