PAT1005 继续(3n+1)猜想,提示Segmentation Fault,已经检查过,也没有
越界呀
#include
int main(){
int n; //save the number's of input
scanf("%d",&n);
int K[100]; //save the test numbers;
int r[100];
int FuGai[100]; //save the fugai numbers
int KeyNum[100]; //save the key numbers
int i; //cyclic variable
for(i=0;i<100;i++){
K[i]=0; //K[100] init value
FuGai[i]=0; //FuGai[100] init value
}
for(i=0;i<n;i++)
scanf("%d",&K[i]);
for(i=0;i<n;i++)
r[i]=K[i];
int j=0,tem,t=0,m=0,y=0;
for(j=0;j<n;j++){
while((K[j]!=0)&&(K[j]!=1)){
if(K[j]%2==0) //even number
{
tem=K[j]/2;
}
if(K[j]%2==1) //odd number
{
tem=(K[j]*3+1)/2;
}
K[j]=tem;
FuGai[t]=K[j];
t++;
}
}
// screen out the key numbers
int flag=1; //if flag==0,not kaynum;else,keynum
for(m=0;m<n;m++){
for(j=0;j<t;j++)
{
if(r[m]-FuGai[j]==0){
flag=0;
break;
}
}
if(flag==1){
KeyNum[y++]=r[m];
}
flag=1;
}
//sort out KeyNum,from max to min
int temp;
for(i=0;i<y-1;i++){
for(j=i;j<y;j++){
if(KeyNum[i]<KeyNum[j]){
temp=KeyNum[j];
KeyNum[j]=KeyNum[i];
KeyNum[i]=temp;
}
}
}
for(i=0;i<y-1;i++)
printf("%d ",KeyNum[i]);
printf("%d",KeyNum[y-1]);
return 0;
}