PAT Level B exercise summary
PAT Level B 1005 Continue (3n+1) Conjecture (25 points) I hope my ideas can help you.Article Directory
1. Question 1005
输入样例:
6
3 5 6 7 8 11
输出样例:
7 6
2. Analysis
Enter up to 100 digits according to the title, and the input number is 1<n<=100; think of using index, take the input number as the array subscript, cut this number n and compare it with the index [n]. If in the index [ If n] exists, then this number is not a key number. Change the position of index [n] to 0; finally remove the number 0 in the index array, fill in the subscripts in turn, and finally output it backwards.
(The demonstration is for reference only, the value of index [n] is changed to 0)
输入后----->
索引:3 5 6 7 8 11
--------->
算3:5 8 4 2 1;
索引:3 0 6 7 0 11
算5:8 4 2 1;
索引:3 0 6 7 0 11
算6:3 5 8 4 2 1;
索引:0 0 6 7 0 11
算7:11 17 26 13 20 10 5 8 4 2 1;
索引:0 0 6 7 0 0
算8:4 2 1;
索引:0 0 6 7 0 0
算11:17 26 13 20 10 5 8 4 2 1;
索引:0 0 6 7 0 0
Shown below 成功代码
// 1005代码
#include<stdio.h>
#define MAXS 101
int main(){
int n,i,j;
scanf("%d",&n);
int num,suo[MAXS]={
0};//索引中初始为0
for(i=0;i<n;i++){
scanf("%d",&num);
suo[num]=1;//以输入值为下标,标记为1
}
for(i=1;i<100;i++){
if(suo[i]){
//如果索引【i】存在
for(j=i;j>1;){
if(j%2==0){
j/=2;
}
else{
j=(3*j+1)/2;
}
if(j<100&&suo[j]){
//索引中有值存在并且j不越界
suo[j]=0;
}
}
}
}
for(i=1,j=1;i<=100;i++){
//从下标1开始
if(suo[i]!=0){
//如果索引【i】不等于0则i为关键字 ,从1开始存
suo[j]=i;
j++;
}//如果索引【i】==0则i++,j不变等着存下一个
}
suo[j]='\0';
printf("%d",suo[j-1]);//按要求输出
for(j=j-2;j>=1;j--){
printf(" %d",suo[j]);
}
return 0;
}