Python 对于较小的项目实例,未获得预期结果
所以我的工作是这样的: 一些正整数n的性质是和Python 对于较小的项目实例,未获得预期结果,python,python-2.7,Python,Python 2.7,所以我的工作是这样的: 一些正整数n的性质是和[n+逆(n)]完全由奇数(十进制)数字组成。例如,36+63=99和409+904=1313。我们称这些数字为可逆的;所以36、63、409和904是可逆的。n或反向(n)中都不允许前导零 1000以下有120个可逆数字 10亿(10**9)以下有多少可逆数 我正在尝试以下代码(并不是使用10^9,而是使用10检查结果(应为零)是否正在发生: def check(x): y = int(str(x)[::-1]) #x backwards
[n+逆(n)]
完全由奇数(十进制)数字组成。例如,36+63=99
和409+904=1313
。我们称这些数字为可逆的;所以36、63、409和904是可逆的。n或反向(n)中都不允许前导零
1000以下有120个可逆数字
10亿(10**9)以下有多少可逆数
我正在尝试以下代码(并不是使用10^9,而是使用10检查结果(应为零)是否正在发生:
def check(x):
y = int(str(x)[::-1]) #x backwards
#add the rev number to the original number (convert them to a list)
xy = list(str(x+y))
#a list of even digits.
evens = ['0', '2', '4', '6', '8']
#check if the number has any digits using intersection method.
intersect = set(xy).intersection(set(evens))
if not intersect:
#if there was no intersection the digits must be all odd.
return True
return False
def firstCheck(x):
if (int(str(x)[:1])+(x%10))%2 == 0:
#See if first number and last number of x make an even number.
return False
return True
def solve():
L = range(1, 10) #Make a list of 10
for x in L:
if firstCheck(x) == False:
#This quickly gets rid of some elements, not all, but some.
L.remove(x)
for x in L:
if check(x) == False:
#This should get rid of all the elements.
L.remove(x)
#what is remaining should be the number of "reversible" numbers.
#So return the length of the list.
return len(L)
print solve()
它分为两部分:在方法
solve
中有一个firstCheck
和check
第一个检查是快速消除一些数字(因此当我制作一个10^9大小的列表时,我可以释放一些RAM)。第二个检查是去除所有假定为“可逆数字”的数字。在第一次检查中,我只看第一个和最后一个数字是否为偶数,然后消除该数字。在检查方法中,我反转该数字,将两个数字相加,并将其放入一个列表,然后检查它是否与偶数列表相交,如果它确实将其从列表中消除。生成的列表应为e“可逆”数字,因此我获取列表并返回其长度。对于范围(1,10)
我得到2作为结果(与所需的零相反)。它没有消除[4,8]的数字,我似乎无法找出原因。我可以看到两个问题
首先,您(两次)修改正在迭代的列表:
for x in L:
if firstCheck(x) == False:
#This quickly gets rid of some elements, not all, but some.
L.remove(x)
这将导致意外和难以预测的行为。可以迭代列表副本,也可以简单地使用列表进行筛选:
L = [x for x in L if firstCheck(x)]
等等
第二,您没有检查以消除任何前导零,因此当检查(10)应为False时,检查(10)为True
>>> len(solve(10))
0
>>> len(solve(1000))
120
[我刚刚添加了一个参数来选择范围。]Java解决方案,需要3分钟
public class Euler145 {
public static void main(String[] args) {
int i, j, add, convert, count = 0;
String get;
StringBuilder rev;
for ( i=0; i <= 1000000000; i++) {
get = "" + i;
rev = new StringBuilder(get);
rev.reverse():
convert = Integer.parseInt(rev.toString());
add = convert + i;
if (add % 2 != 0) {
while (add > 0) {
j = add % 10;
if (j == 1 || j == 3 || j == 5 || j == 7 || j == 9) {
add /= 10;
String leadZero = "" + i;
if (j == 0 && !leadZero.endsWith("0")) {
count++;
}
} else {
break;
}
}
}
}
System.out.println(count);
}
}
公共类Euler145{
公共静态void main(字符串[]args){
int i,j,add,convert,count=0;
字符串获取;
StringBuilder版本;
对于(i=0;i=0){
j=添加%10;
如果(j==1 | | j==3 | | j==5 | | j==7 | | j==9){
加/=10;
字符串leadZero=”“+i;
如果(j==0&&!leadZero.endsWith(“0”)){
计数++;
}
}否则{
打破
}
}
}
}
系统输出打印项次(计数);
}
}
@anon:“前导零在n或反向(n)中都是不允许的。”“哦,等等,我误读了,我以为它说的是,就像前导零一样,它们也不允许存在,所以请删除零,然后执行……PS:“something==False”比“notsomething”更不符合python的意思。注意:1.您可以通过计数而不是管理列表来避免任何空间问题,这也更快。2.这种方法速度慢,而且扩展性不好。通过分析条件,您可以得到一个即时返回答案的解决方案,即使对于10^20这样的边界。这个问题与Python有关