题目:输入一个输n,从1 2 3.......n数,数到3的退出.
红色的地方是两个程序不的同地方,我认为两个效果是一样的……但结果总是不一样……第一个是正确的
为什么?????
/*第一个*/
#define nmax 50
main()
{
int i,j,k,m,n,num[nmax],*p;
printf("please input the total of numbers:");
scanf("%d",&n);
p=num;
for(i=0;i<n;i++)
*(p+i)=i+1;
k=0;
m=0;
for(i=0;m<n-1;i++)
{if(i==n) i=0;
if(*(p+i)!=0) k++;
if(k==3)
{printf("%2d is out!",*(p+i));
*(p+i)=0;
k=0;
m++;
printf("The new order is :");
for(j=0;j<n;j++)
if(*(p+j)!=0)
printf("%2d ",*(p+j));
printf("\n");
}
}
while(*p==0) p++;
printf("%d is left\n",*p);
printf("Game over!");
}
/*第二个*/
#define nmax 50
main()
{
int i,j,k,m,n,num[nmax],*p;
printf("please input the total of numbers:");
scanf("%d",&n);
p=num;
for(i=0;i<n;i++)
*(p+i)=i+1;
k=0;
m=0;
for(i=0;m<n-1;i++)
{if(i==n) i=0;
if(*(p+i)!=0) k++;
if(k%3==0)
{printf("%2d is out!",*(p+i));
*(p+i)=0;
m++;
printf("The new order is :");
for(j=0;j<n;j++)
if(*(p+j)!=0)
printf("%2d ",*(p+j));
printf("\n");
}
}
while(*p==0) p++;
printf("%d is left\n",*p);
printf("Game over!");
}