java 杭电3784继续xxx定律 栈的使用

继续xxx定律

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2909    Accepted Submission(s): 836


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
 
  
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
 

Source
 

Recommend
notonlysuccess   |   We have carefully selected several similar problems for you:   3786  4008  1325  1881  1880 



感受被赵同学支配的恐惧吧:

package com.it.method;


import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
/*
 * 送一组测试数据 10 
 * 1 2 3 4 5 6 7 8 9 10
 * 输出9 6
 * 所以后边就不用解释了
 */
//public class Main {
public class Test03
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
int m = cin.nextInt();
if(m==0)
break;
int a[]= new int[m];
List<Integer>q = new LinkedList<Integer>();//创建一个list集合
Stack<Integer>s = new Stack<Integer>();//创建一个 栈
for(int i=0;i<m;i++)
{
a[i] = cin.nextInt();//读入数据
int n=a[i];
if(!q.contains(n))//如果集合里边还没有n那么 一定要检测n
s.push(n);
else//如果已经 有了  下边就不需要进行了
continue;
while(n!=1)
{
//q.add(n);// 有重复数据,所以这一行  要放在 下边
if(n%2==1)
n=(n*3+1)/2;
else
n/=2;
q.add(n);
}
}
int ans[] = new int [1100];//详情 看前边注释,是检测最少的 
int cnt=0;
while(!s.isEmpty())
{
if(!q.contains(s.peek()))
ans[cnt++] = s.peek();
s.pop();
}
for(int i=0;i<cnt-1;i++)
System.out.print(ans[i]+" ");
System.out.println(ans[cnt-1]);
s.clear();
q.clear();
}
cin.close();
}


}

猜你喜欢

转载自blog.csdn.net/z8110/article/details/52729749
今日推荐