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
1<=n<=500
1<a[i]<=1000
Output
请计算并输出数组a中包含的关键数,并按照其输入顺序的逆序输出,每个用例输出占一行。
Sample Input
3 3 8 4 5 3 8 4 7 15 5 3 8 4 15 7 0
Sample Output
3 15 7 3 7 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; }