什么是编码此问题的正确方法

时间:2019-06-12 20:45:23

标签: python-3.x

您有x号。 5卢比硬币和y号。 1卢比硬币。您要购买金额为z的商品。店主希望您提供准确的零钱。您要使用最小数量的硬币支付。您将使用多少5卢比硬币和1卢比硬币?如果无法进行精确更改,则显示-1。下面的代码部分由我完成。

def make_amount(rupees_to_make, no_of_five, no_of_one):
    five_needed = 0
    one_needed = 0
    if(no_of_five > five_needed and no_of_one > one_needed):
        five_needed = no_of_five - rupees_to_make % 5
        one_needed = rupees_to_make % 5
    rupees_to_make = five_needed * 5 + one_needed * 1

    print("No. of Five needed :", five_needed)
    print("No. of One needed  :", one_needed)
    print(-1)


# Provide different values for rupees_to_make, no_of_five, no_of_one and test your program
make_amount(28,8,5)

6 个答案:

答案 0 :(得分:2)

infyTQ Assignment

你有 x 没有。 5 卢比硬币和 y 没有。 1 卢比硬币。您想购买数量为 z 的商品。店主希望您提供准确的零钱。您想使用最少数量的硬币付款。您将使用多少 5 卢比硬币和 1 卢比硬币?如果无法进行精确更改,则显示 -1。下面的代码部分由我完成。

def make_amount(rupees_to_make, no_of_five, no_of_one):

    if rupees_to_make <= (no_of_five * 5)+(no_of_one * 1):
        five_needed = (rupees_to_make) // 5
        if five_needed > no_of_five:
            five_needed = no_of_five
        one_needed = (rupees_to_make)-(five_needed * 5)
        if no_of_one < one_needed:
            print("-1")
        else:
            rupees_to_make = (five_needed * 5 ) +( one_needed * 1)

            print("No. of Five needed :", five_needed)
            print("No. of One needed  :", one_needed)


    else:
        print("-1")

rupees_to_make = int(input())
no_of_five = int(input())
no_of_one = int(input())

min_coins_needed = make_amount(rupees_to_make, no_of_five, no_of_one)

答案 1 :(得分:1)

同样的问题用Java解决

<块引用>

您有 x 张 5 美元纸币和 y 张 1 美元纸币。您想购买数量为 z 的商品。店主希望您提供准确的零钱。您想使用最少数量的纸币付款。您将使用多少张 5 美元纸币和 1 美元纸币?执行一个程序来找出将使用多少 5 美元纸币和 1 美元纸币。如果无法进行精确更改,则显示 -1。

class Notes 
{
       public static void main(String[] args) 
       {
        
        int Note1 = 2,Note5 = 6;
        int purchaseAmt = 35;
        int money = (Note1 * 1) + (Note5 * 5); 
        if(money < purchaseAmt)
        {
            System.out.println(-1);
            
        }
        else
        {
            int Note5Need = purchaseAmt/5; 
            
            if(Note5Need > Note5 )
            {
                Note5Need = Note5;
            } 
            
            int Note1Need = purchaseAmt % Note5Need;   // 21 % 4 = 1  27 % 5 =2
            if(Note1Need < Note1 ) //2 < 5
            {
                  System.out.println("$1 notes needed = "+Note1Need);
                  System.out.println("$5 notes needed = "+Note5Need);
            }
            else 
            {
                System.out.println(-1);
                
            }
        }
        // Implement your code here 
    }
}

答案 2 :(得分:0)

可能可以找到一个更具动态性的解决方案,但只有5s和1s可以很好地工作,另外,使其更具动态性也可能使它更难阅读,并且对于两种不同的货币面额来说不必要地复杂:

def make_amount(rupees_to_make, no_of_five, no_of_one):
    remaining_fives = no_of_five # We need variable to track remaining
    remaining_ones = no_of_one

    five_needed = rupees_to_make // 5 # number of fives needed
    one_needed = rupees_to_make % 5 # number of ones eneded

    if five_needed > remaining_fives: # If we need more fives than we have
        one_needed += 5 * (five_needed - remaining_fives) # Then we need more ones
        remaining_fives = 0 # and we use all our fives
    else:
        remaining_fives -= five_needed # we have enough fives so use them

    if one_needed > remaining_ones:
        return -1 # We don't have enough ones
    else:
        remaining_ones -= one_needed # remove the used ones

    return f"Fives {no_of_five - remaining_fives}, Ones {no_of_one - remaining_ones}"


# Provide different values for rupees_to_make, no_of_five, no_of_one and test your program
print(make_amount(29,8,5))

输出:

Fives 5, Ones 4

答案 3 :(得分:0)

getCompletedAssessmentActionItems(): Observable<IAssessmentFilterValueOption[]> {
    return this.httpClient
      .get<IAssessmentFilterValueOption[]>(
        'api/assessment-v2/assessment-results'
      )
      .pipe(
        catchError(
          this.aaResponseHandlerService.error(this.translateService.instant('ErrorFetchingFilters'))
        )
      );
  }

make_amount(28,5,3)

答案 4 :(得分:0)

def cleanLDAP(search):
    escChars = {'(':r'\28', ')':r'\29', r'\\': r'\5c' }
    for ch, val in escChars.items():
        if ch in search:
            search = search.replace(ch, val)
    return search

>>> cleanLDAP('(123)')

#'\\28123\\29'

答案 5 :(得分:0)

def make_amount(rupees_to_make,no_of_five,no_of_one):
    five_needed=0
    one_needed=0

    #Start writing your code here
    total = (no_of_one*1)+(no_of_five*5)
    if(rupees_to_make > total):
        print("-1")
    else:
        five_needed = rupees_to_make // 5
        if(five_needed > no_of_five):
            five_needed = no_of_five
        one_needed = rupees_to_make - (five_needed*5)
        if(one_needed > no_of_one):
            print("-1")
        else:
            print("No. of Five needed :", five_needed)
            print("No. of One needed  :", one_needed)