首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

HDU3784:持续xxx定律

2013-03-06 
HDU3784:继续xxx定律Problem Description当n为3时,我们在验证xxx定律的过程中会得到一个序列,3,5,8,4,2,1,

HDU3784:继续xxx定律
Problem Description当n为3时,我们在验证xxx定律的过程中会得到一个序列,3,5,8,4,2,1,将3称为关键数,5,8,4,2称为覆盖数。现在输入n个数字a[i],根据关键数与覆盖数的理论,我们只需要验证其中部分数就可以确定所有数满足xxx定律,输出输入的n个数中的关键数。如果其中有多个关键数的话按照其输入顺序的逆序输出。 
Input输入数据包含多个用例,每个用例首先包含一个整数n,然后接下来一行有n个整数a[i],其中:
1<=n<=500
1<a[i]<=1000 
Output请计算并输出数组a中包含的关键数,并按照其输入顺序的逆序输出,每个用例输出占一行。 
Sample Input

33 8 453 8 4 7 1553 8 4 15 70
 
Sample Output
315 7 37 15 3
 


 

 

 

#include <iostream>#include <string.h>#include <cstdio>using namespace std;int main(){    int n;    int a[1005],i,flag[1005];    while(cin >> n && n)    {        memset(flag,0,sizeof(flag));        for(i = 0; i<n; i++)        {            cin >> a[i];            flag[a[i]] = 1;        }        for(i = 0; i<n; i++)        {            int t;            t = a[i];            if(!flag[t])                continue;            while(t>1)            {                if(t%2)                    t = (t*3+1)/2;                else                    t = t/2;                if(t<=1000)                {                    flag[t] = 0;                }            }        }        int k = 1;        for(i = n-1; i>=0; i--)        {            if(flag[a[i]])            {                if(k)                {                    cout << a[i];                    k = 0;                }                else                {                    cout << " " << a[i];                }            }        }        cout << endl;    }    return 0;}


 

热点排行