您的位置:首页 > 编程语言 > Python开发

PAT Basic 1005

2014-10-21 11:37 169 查看

今天的此时此刻是一个特殊的时刻,在这一时刻终于攻克了PAT Basic Level的所有试题,想来从最初接触这个Programming Ablitity Test以来,被题目中的每一个坑深深的坑过,幸好挺过来了,请看第五题的解答,今天心情好说一说程序的思路。那一天我以为使用Python解决这道题很容易,但是陷入了一个Python的误解中……开始的时候总是以为链表的reverse就是逆序输出,其实不然,这个函数只是将链表的内容逆序而已,所以,在调用这个函数之前又排了一次序。


1005. 继续(3n+1)猜想 (25)

时间限制

400 ms

内存限制

32000 kB

代码长度限制

8000 B

判题程序

Standard

作者

CHEN, Yue

卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里,情况稍微有些复杂。

当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。例如对n=3进行验证的时候,我们需要计算3、5、8、4、2、1,则当我们对n=5、8、4、2进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这4个数已经在验证3的时候遇到过了,我们称5、8、4、2是被3“覆盖”的数。我们称一个数列中的某个数n为“关键数”,如果n不能被数列中的其他数字所覆盖。

现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。

输入格式:每个测试输入包含1个测试用例,第1行给出一个正整数K(<100),第2行给出K个互不相同的待验证的正整数n(1<n<=100)的值,数字间用空格隔开。

输出格式:每个测试用例的输出占一行,按从大到小的顺序输出关键数字。数字间用1个空格隔开,但一行中最后一个数字后没有空格。
输入样例:
6
3 5 6 7 8 11

输出样例:
7 6

#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on 2014年9月16日

@author: qcq
acquired help from: cxp

The description of this file:
#English#
The reason to write this file, what contains in this file. What is the most import function in this file or
what specific algorithm implemented in this file's code.

N: The important common parameters should list here.

The change records:
Time     Changed By    Reason
2014年9月16日        qcq
...              ...        ...
'''
def CutNumber(temp):
global data
data.append(temp)
while temp > 1:
if 0 == temp % 2:
temp = temp / 2
data.append(temp)
else:
temp = (3 * temp + 1) / 2
data.append(temp)

if __name__ == '__main__':
data = []
temp1 = []
a = int(raw_input().strip())
b = raw_input().strip().split(' ')
for index in range(a):
data = []
for index_2 in range(a):
if index_2 != index:
CutNumber(int(b[index_2]))
if data.count(int(b[index])):
break;
if  0 == data.count(int(b[index])):
temp1.append(int(b[index]))
temp1.sort()
temp1.reverse()
string = ''
for i in temp1:
string+=str(i) + ' '
string = string[0:len(string) - 1]
print string


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT basic python