http://acm.hdu.edu.cn/showproblem.php?pid=1279

按要求模拟

View Code
#include <stdio.h>
int main()
{
    int t,n,p,f,f1;
    scanf("%d",&t);
    while(t--)
    {
        f=f1=1;
        scanf("%d",&n);
        p=n;
        while(n)
        {
            if(n%2&&n!=1){
                f1=0;
                break;
            }
            n>>=1;
        }
        while(1)
        {
            if(p==1)break;
            if(f&&p%2&&p!=1)
            {
                printf("%d",p);
                f=f1=0;
            }
            else if(f==0&&p%2) 
                printf(" %d",p);
            if(p%2)p=p*3+1;
            else p>>=1;
        }
        if(f1)
            printf("No number can be output !");
        putchar('\n');
    }
    return 0;
}