追蹤者

2018年9月6日 星期四

Python 商管程設(一) 第三周作業

題目敘述
如果你在一家零售店幫消費的客人結帳,你可能需要快速地挑出合適且數量正確的鈔票與零錢。假設客人的消費金額 aa 一定是 1 到 1000 之間的整數,而你有無限量的 500、100、50、10、5、1 這些面額的鈔票和零錢,我們希望你能依照下面的規則找錢:

你找的錢的總額要是 1000 - a1000−a。
與其給客人五張 100 元,不如給他一張 500 元;與其給客人兩個 50 元,不如給他一張 100 元……依此類推。
以下是一些範例:

如果客人消費 200 元,你應該找給他 1 張 500 元和 3 張 100 元。
如果客人消費 286 元,你應該找給他 1 張 500 元、2 張 100 元、1 個 10 元和 4 個一元。
如果客人消費 925 元,你應該找給他 1 個 50 元、2 個 10 元和 1 個 5 元。
在本題中,你將會被給予上述的整數 aa,而你要找出符合上述規則的唯一找錢方式。

輸入輸出格式
在每筆測試資料中,會有一個整數 aa 代表客人的消費金額,aa 會介於 1 到 999 之間(包含 1 跟 999)。讀入 aa 之後,你會依照題目指定的規則找出每種面額的鈔票或銅板應該要給幾張或幾個,然後由面額大至面額小依序輸出所需鈔票張數或銅板個數,但如果不應該找給客人某個面額的鈔票或銅板,就跳過該面額不要輸出。因為這樣一來可能只輸出少於 6 個數字,會不知道怎麼對應到面額,因此現在要把面額與所需張數(個數)成對地輸出,中間用一個逗點和一個空格隔開,而面額與面額之間用一個分號和一個空格隔開。



**************
CODE :
a = int(input()) # a介於1~999
tmp = 1000 - a
cnt = 0 # 計算非零面額數
p1 = tmp // 500
if p1 != 0:
cnt += 1
tmp = tmp - p1 * 500
p2 = tmp // 100
if p2 != 0:
cnt += 1
tmp = tmp - p2 * 100
p3 = tmp // 50
if p3 != 0:
cnt += 1
tmp = tmp - p3 * 50
p4 = tmp // 10
if p4 != 0:
cnt += 1
tmp = tmp - p4 * 10
p5 = tmp // 5
if p5 != 0:
cnt += 1
tmp = tmp - p5 * 5
p6 = tmp
if p6 != 0:
cnt += 1


if p1 != 0:
print("500, "+str(p1), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p2 != 0:
print("100, "+str(p2), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p3 != 0:
print("50, "+str(p3), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p4 != 0:
print("10, "+str(p4), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p5 != 0:
print("5, "+str(p5), end="")
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")
if p6 != 0:
print("1, "+str(p6), end="")
p6 = 0
cnt -= 1
if cnt != 0:
print("; ", end="")
else:
print("", end="")

*** End Code ***
其實P6 那也可以不用判斷cnt 直接印出空白









7 則留言:

  1. a = 1000-int(input())
    五百元 = a // 500
    一百元 = (a%500) // 100
    五十元 = (a%100) // 50
    十元 = (a%50) // 10
    五元 = (a%10) // 5
    一元 = (a%5)

    if 五百元 != 0:
    print("500, " + str(五百元) + "; ", end="")
    if 一百元 != 0:
    print("100, " + str(一百元) + "; ", end="")
    if 五十元 != 0:
    print("50, " + str(五十元) + "; ", end="")
    if 十元 != 0:
    print("10, " + str(十元) + "; ", end="")
    if 五元 != 0:
    print("5, " + str(五元) + "; ", end="")
    if 一元 != 0:
    print("1, " + str(一元))

    回覆刪除
    回覆
    1. 這個code 會出現的問題是若最後一項非一元面額的話會多出一個分號。
      以下是我稍作修改的code:
      x = 1000-int(input())
      ans1 = x // 500
      ans2 = (x % 500) // 100
      ans3 = (x % 100) // 50
      ans4 = (x % 50) // 10
      ans5 = (x % 10) // 5
      ans6 = (x % 5)
      sum = 0 # 非零面額總數
      if ans1 != 0:
      sum += ans1
      if ans2 != 0:
      sum += ans2
      if ans3 != 0:
      sum += ans3
      if ans4 != 0:
      sum += ans4
      if ans5 != 0:
      sum += ans5
      if ans6 != 0:
      sum += ans6

      if ans1 != 0:
      sum -= ans1
      if sum != 0:
      print("500, " + str(ans1) + "; ", end = "")
      else:
      print("500, " + str(ans1), end = "")
      if ans2 != 0:
      sum -= ans2
      if sum != 0:
      print("100, " + str(ans2) + "; ", end = "")
      else:
      print("100, " + str(ans2), end = "")
      if ans3 != 0:
      sum -= ans3
      if sum != 0:
      print("50, " + str(ans3) + "; ", end = "")
      else:
      print("50, " + str(ans3), end = "")
      if ans4 != 0:
      sum -= ans4
      if sum != 0:
      print("10, " + str(ans4) + "; ", end = "")
      else:
      print("10, " + str(ans4), end = "")
      if ans5 != 0:
      sum -= ans5
      if sum != 0:
      print("5, " + str(ans5) + "; ", end="")
      else:
      print("5, " + str(ans5), end = "")
      if ans6 != 0:
      print("1, " + str(ans6), end = "")

      刪除
  2. a = 1000
    b = int ()
    b = int (input("請輸入金額"))
    x=a-b
    print(x//500,(x%500)//100,(x%100)//50,(x%50)//10,(x%10)//5,(x%5)//1)

    回覆刪除
  3. 以下分享一下我的code:
    charge = 1000 - int(input())
    #calculate each variable at first.
    a = charge // 500
    b = (charge % 500) // 100
    c = (charge % 100) // 50
    d = (charge % 50) // 10
    e = (charge % 10) // 5
    f = (charge % 5)


    if a == 0:
    print("", end = "")
    else:
    if b == 0 and c == 0 and d == 0 and e == 0:
    print("500, " + str(a) ,end = "")
    else:
    print("500, " + str(a)+"; " ,end = "")
    if b == 0:
    print("", end = "")
    else:
    if c == 0 and d == 0 and e == 0 and f == 0:
    print("100, " + str(b) ,end = "")
    else:
    print("100, " + str(b)+"; " ,end = "")
    if c == 0:
    print("", end = "")
    else:
    if d == 0 and e == 0 and f == 0:
    print("50, " + str(c) ,end = "")
    else:
    print("50, " + str(c)+"; " ,end = "")
    if d == 0:
    print("", end = "")
    else:
    if e == 0 and f == 0:
    print("10, " + str(d) ,end = "")
    else:
    print("10, " + str(d)+"; " ,end = "")
    if e == 0:
    print("", end = "")
    else:
    if f == 0:
    print("5, " + str(e),end = "")
    else:
    print("5, " + str(e)+"; ",end = "")
    if f == 0:
    print("", end = "")
    else:
    print("1, " + str(f) ,end = "")

    回覆刪除
  4. 作者已經移除這則留言。

    回覆刪除
  5. https://hackmd.io/@buddin/HkPFoeCg2
    #include
    #include
    #define N 7
    int main(){
    int x;
    scanf("%d", &x);
    int y[] = {x/50, (x%50)/10, (x%10)/5, x%5};
    char s1[50]={'\0'}, s2[N]={'\0'}, s3[N]={'\0'}, s4[N]={'\0'};
    if (y[0]) sprintf(s1, "%d*$50 ", y[0]);
    if (y[1]) sprintf(s2, "%d*$10 ", y[1]);
    if (y[2]) sprintf(s3, "%d*$5 ", y[2]);
    if (y[3]) sprintf(s4, "%d*$1 ", y[3]);
    strcat(s1,s2);
    strcat(s1,s3);
    strcat(s1,s4);
    s1[strlen(s1)-1]='\0';
    printf("%s", s1);
    }

    回覆刪除