为什么两个输出完全相同且正确的程序的其中一个会WA(20pts),可另一个程序会AC?
WA code:
#include<bits/stdc++.h>
using namespace std;
int a[10000],n,idx;
int main(){
scanf("%d",&n);
a[0]=n;
while(a[idx-1]!=1)a[++idx]=a[idx-1]%2==0?a[idx-1]/2:a[idx-1]*3+1;
while(idx--)printf("%d ",a[idx]);
return 0;
}
AC code:
#include<bits/stdc++.h>
using namespace std;
int a[10000],n,idx;
int main(){
scanf("%d",&n);
a[0]=n;
while(a[idx]!=1){
idx++;
if(a[idx-1]%2==0)a[idx]=a[idx-1]/2;
else a[idx]=a[idx-1]*3+1;
}
for(int i=idx;i>=0;i--)printf("%d ",a[i]);
return 0;
}
求解答