pynliu
发表于 2017-9-7 11:35:50
def func(num):
resp = []
for i in range(len(bin(num))-2):
if bin(num)[-i - 1] == '1':
resp.append(str(2**i))
return ' + '.join(resp)
goog
发表于 2017-9-7 12:01:13
base=
def fun(number):
member = []
for i in base:
if i&number:
member.append(i)
return member
number = int(raw_input('请输入1-255之间的任意整数:'))
tem = fun(number)
equation = str(number) + '='
for i in tem:
if tem.index(i) != len(tem)-1:
equation +=str(i) + ' + '
else:
equation +=str(i)
print equation
schweinfan
发表于 2017-9-7 17:16:26
本帖最后由 schweinfan 于 2017-9-7 17:27 编辑
def fun(num):
ans = []
for i in :
if i <= num:
num = num - i
ans.insert(0, str(i))
return '+'.join(ans)
输出:
>>> print(fun(4))
4
>>> print(fun(70))
2+4+64
>>> print(fun(255))
1+2+4+8+16+32+64+128
Richard_bin
发表于 2017-9-7 22:37:03
good
Kuroi幻月
发表于 2017-9-7 23:07:35
def fun(n):
group =
for i in group:
if n >= i:
if n == i:
return "%d" % i
else:
return (("%d + "% i)+fun(n-i))
n = 155
print(fun(n))
结果:
======
128 + 16 + 8 + 2 + 1
>>>
虽然我是从128开始输出的,但也应该可以反映出结果吧
wc365
发表于 2017-9-7 23:53:37
def fun(num):
str1 = ''
for n in :
if num >= n:
str1 = str(n) + '+' + str1
num = num - n
return str1[:-1]
鱼油9527
发表于 2017-9-8 00:04:46
新人报到,支持鱼C
liuwenqi
发表于 2017-9-8 10:28:48
def parse():
while True:
i = 0
num = input('请输入一个数:(按q或Q退出)')
if num == 'q' or num == 'Q':
break
elif int(num) < 0 or int(num) > 255:
print('输入的数在(0, 255)之间')
continue
else:
b_num = bin(int(num)).split('0b')
for ch in reversed(b_num):
if ch == '1':
print(2**i, end='')
if i < len(b_num)-1:
print('+', end='')
i += 1
print( '=%d\n' % int(num))
print('退出\n')
parse()
玄天宗
发表于 2017-9-8 15:22:57
学习
编程新血
发表于 2017-9-8 20:23:26
num=int(input('输入数字0~256:'))
if num==0:
print('0=0')
else:
x=list(bin(num))
x=x[::-1]
v=
print(num,end='=')
for i in range(len(x)):
v=2**i*int(x)
for i in v:
if i!=0 and i!=max(v):
print(i,end='+')
if i==max(v) and i!=0:
print(i)
仝相杰
发表于 2017-9-9 09:18:10
我是来看答案的
faye_vip
发表于 2017-9-9 10:35:24
def fun(num):
numbers =
result = []
for number in numbers:
while num >= number:
result.append(str(number))
num = num - number
break
result = list(reversed(result))
return ' + '.join(result)
麦卡鲁
发表于 2017-9-9 10:58:43
本帖最后由 麦卡鲁 于 2017-9-9 11:01 编辑
适用于任意整数的程序:
#/usr/bin/env python3.6
# -*- coding: utf-8 -*-
"""
@version: python 3.6.1
@author: zengyongjie
@contact: zengyongjievip@163.com
@software: PyCharm Community Edition
@file: 拆分数字.py
@time: 2017/9/9 上午10:24
"""
'''
给定一个1-255之间的数字,拆分为2的幂次方相加的序列
'''
def cal_max_power(num):
count = 0
while num // 2:
count += 1
num //= 2
return count
def get_power_list(num):
power_list = []
while num:
power_list.append(cal_max_power(num))
num = num - pow(2, cal_max_power(num))
return power_list
def print_serial(num):
power_list = get_power_list(num)
result = []
for each_power in power_list:
result.append(str(pow(2, each_power)))
print('+'.join(result))
if __name__ == '__main__':
while True:
try:
num = int(input('请输入任意正整数:').strip())
except TypeError as reason:
print('输入错误,请重新输入。')
else:
break
print_serial(num)
运行结果:
请输入任意正整数:13654
8192+4096+1024+256+64+16+4+2
请输入任意正整数:189
128+32+16+8+4+1
请输入任意正整数:255
128+64+32+16+8+4+2+1
请输入任意正整数:36
32+4
请输入任意正整数:3
2+1
YunOS
发表于 2017-9-9 11:17:12
def fun(num):
num_list = map(lambda x: 2**x, range(8))
y = {}
flag = True
count = -1
x = 0
if num > 255 or num < 1:
print "Error input!"
return 0
for i in range(8):
if num == 0:
break
if num - x >= num_list:
if flag:
flag = False
count = 7-i
y = 1
x += num_list
else:
y = 0
# Concatenate the string into one.
str1 = ""
for i in range(count):
#print str(i) + ":" + str(y)
if y == 1:
str1 += str(num_list) + " + "
str1 += str(num_list)
return str1
龍三少
发表于 2017-9-9 16:18:09
// 鱼C92.cpp : Defines the entry point for the console application.
//
/*
1, 2, 4, 8, 16, 32, 64, 128这8个数字利用加法可以得到1~255中的任意数字
如 21 = 1 + 4 + 16; 192 = 64 + 128;
编写一个函数输入1~255的数字,给出如何利用1, 2, 4, 8, 16, 32, 64, 128的
加法可以得到这个数字
*/
/*本人小白,学c语言不久,给出的答案可能不完美,轻喷,另求指正*/
#include "stdafx.h"
#include <stdio.h>
void ret (int x, int y)//计算结果
{
int rem; //余数
//int a = {0};
printf("%d = ", x);
rem = x % y;
printf(" %d", y);
y /= 2;
do
{
if (rem < y)
{
y /= 2;
}
else
{
rem %= y;
printf(" + %d", y);
y /= 2;
}
}while(rem);
}
//void ret (int x, int rem);
int main(int argc, char* argv[])
{
int n = 0;
int b = {1, 2, 4, 8, 16, 32, 64, 128};
int t = 0;
puts("请输入一个1~255之间的数字,1, 2, 4, 8, 16, 32, 64, 128这8个数字除外");
scanf("%d", &n);
for(int i = 0; i < 8; ++i)//对数组b进行遍历,排除用户输入的非法数字
{
if (n == b)
t = 1;
}
if(t)
{
printf("%d无法利用1, 2, 4, 8, 16, 32, 64, 128这8个数字加法得到\n", n);
}
else
{
if (n < 4)
{
ret (n, 2);
printf("\n");
}
else if (n < 8)
{
ret (n, 4);
printf("\n");
}
else if(n < 16)
{
ret (n, 8);
printf("\n");
}
else if (n < 32)
{
ret (n, 16);
printf("\n");
}
else if (n < 64)
{
ret (n, 32);
printf("\n");
}
else if (n < 128)
{
ret (n, 64);
printf("\n");
}
else if (n <= 255)
{
ret (n, 128);
printf("\n");
}
}
return 0;
}
mmdn
发表于 2017-9-9 19:16:25
def fun(num):
return ' + '.join( == '1'])
mmdn
发表于 2017-9-9 19:17:08
看看
PythonHanoi
发表于 2017-9-9 22:18:24
L=
s=[]
def function(num):
for number in L:
if num-number<0:
continue
num = num-number
s.append(str(number))
print('+'.join(s))
AKA2pac
发表于 2017-9-10 11:31:18
def fun(num):
mod =
s = []
for i in mod:
if num >= i:
num = num - i
s.append(str(i))
s.reverse() # 128+64+8+4+1
return '+'.join(s)
谁动了我的豆包
发表于 2017-9-10 12:14:52
def fun(num):
result = []
list1 =
i = len(list1)
while i > 0 and num > 0:
if num >= list1:
result.append(list1)
num -= list1
i -= 1
else:
i -= 1
print(' + '.join(str(i) for i in result))