1. 程式人生 > >【PAT】B1005 繼續(3n+1)猜想(25 分)

【PAT】B1005 繼續(3n+1)猜想(25 分)

tdi can scanf code ret return amp emp ++

#include<stdio.h>
#include<algorithm>
using namespace std;
int Table[1000]={0};
bool hashtable[105];
bool cmp(int a,int b){ return a>b;}
void put(){
    int temp;scanf("%d",&temp);
    Table[temp]++;hashtable[temp]=true;
    if(Table[temp]>1) return;
    while(temp!=1){
        if(temp%2==0){
            temp/=2;
            if(temp<=100) Table[temp]++;
        }else{
            temp=(temp*3+1)/2;
            if(temp<=100) Table[temp]++;
        }
    }
}
int main(){
    int N,shuchu[100],num=0;
    scanf("%d",&N);
    for(int i=0;i<N;i++){ put(); }
    for(int i=2;i<=100;i++){
        if(Table[i]==1&&hashtable[i]==true) shuchu[num++]= i;
    }
    if(num!=0) sort(shuchu,shuchu+num,cmp);
    for(int i=0;i<num;i++){
        if(i!=0)printf(" ");
        printf("%d",shuchu[i]);
    }
    return 0;
}

【PAT】B1005 繼續(3n+1)猜想(25 分)