在正数和负数之间插入数字

时间:2019-12-22 05:56:42

标签: python-3.x

请建议如何将5加上负数,以使它的值最小。

我设法使其仅在正数上起作用,但是需要使它在两种情况下都起作用,如果您能建议如何使它在负数上起作用,请多加赞赏。

def solution(N):
    a = [] # list of digits, e.g. int(123)
    while N != 0:
        v = N % 10  # last digit as div remainder, e.g.: 123 % 10 = 3
        N = int(N / 10)  # remove last digit using integer division: 123 / 10 = 12.3; int(12.3) = 12
        a = [v] + a  # concatenate two lists: newly created list with one element (v = 3) and list a 
# as a result the digits will be in natural order => [1,2,3]
    if len(a) == 0:   # need to create list with one element [0] as the cycle before ignores 0
        a = [0]
    inserted = False
    for i in range(0, len(a)):   # i = 0, 1, 2; len = 3
        if a[i] < 5:
            # a[from:to exclusive] e.g.: [1, 2, 3][0:2] => [1, 2]. index of 1 is 0, index of 2 is 1, index 2 is excluded
            a = a[0:i] + [5] + a[i:]
            inserted = True
            break
    if not inserted:
        a = a + [5]
    N = 0  # reconstruct number from digits, list of digits to int
    for i in range(0, len(a)):
        N = N * 10 + a[i]  # 0 + 1; 1 * 10 + 2; 12 * 10 + 3 = 123
    return N 

if __name__ == ‘__main__’:
    print(“Solution:”, solution(0))

4 个答案:

答案 0 :(得分:1)

在这里,我通过使用一些内置的python方法进行了一些重大更改:

def solution(N):

    sign = False   #to determine the sign of N (positive or negative )
    if N < 0:
       sign  = True
       N= N * -1   # as N<0 we make it positive

    a = []
    while N != 0:
        v = N % 10
        N = int(N / 10)
        a = [v] + a

    a.append(5)  # in built method to add an element at the end of the list 
    a.sort()     # in built method to sort the list (ascending order)
    a.reverse()  # in build method to reverse the order of list (make it descending order)

    N = 0
    for i in range(0, len(a)):
        N = N * 10 + a[i]

    if sign:    # convert negative integers back to negative 
       N = N * -1 

    return N

示例输出:

否定

solution(-2859)
-98552

正面

solution(9672)
97652

答案 1 :(得分:1)

下面将解决问题的方法:

x=-34278
no_to_insert=5
res=int(''.join(sorted(list(str(abs(x)))+[str(no_to_insert)], reverse=True)))
if x<0:
    res=-res

输出:

-875432

答案 2 :(得分:1)

Java 解决方案

 public int solution(int N) {
            int digit = 5;
            if (N == 0) return digit * 10;
            int neg = N/Math.abs(N);
            N = Math.abs(N);
            int n = N;
            int ctr = 0;
            while (n > 0){
                ctr++;
                n = n / 10;
            }
            int pos = 1;
            int maxVal = Integer.MIN_VALUE;
            for (int i=0;i<=ctr;i++){
                int newVal = ((N/pos) * (pos*10)) + (digit*pos) + (N%pos);
                if (newVal * neg > maxVal){
                    maxVal = newVal*neg;
                }
                pos = pos * 10;
            }
            return maxVal;
        }

答案 3 :(得分:0)

如果您需要插入5并使输出数字成为正数和负数的最大值(并且没有不替换或转换输入数字集的条件),那么这可能是一个解决方案:

def solution(N):

    negative = False

    if N < 0:
        negative = True
        N = N * -1   # as N<0 we make it positive

    a = []
    while N != 0:
        v = N % 10
        N = int(N / 10)
        a = [v] + a

    if len(a) == 0:
        a = [0]

    inserted = False
    for i in range(0, len(a)):
        if (not negative and a[i] < 5) or (negative and a[i] > 5):
            a = a[0:i] + [5] + a [i:]
            inserted = True
            break

    if not inserted:
        a = a + [5]

    N = 0
    for i in range(0, len(a)):
        N = N * 10 + a[i]

    if negative:
       N = N * -1

    return N

if __name__ == '__main__':
    print("Solution:", solution(N))