1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

最適お釣り計算。

Last updated at Posted at 2019-06-23

お釣りの金額に対して、
現在の日本の通貨何枚で払えばよいか、計算します。

cc curr.c -o currとして、コンパイルして、
./curr <金額>として、実行して下さい。

単純なアルゴリズムです。2千円札は、話がややこしくなるので、扱いません。

curr.c
#include  <stdio.h>
int main(int argc,char *argv[]) {
    int a;
    int c10000,c5000,c1000,c500,c100,c50,c10,c5,c1;
    if (argc==2)
      sscanf(argv[1],"%d",&a);
    else return 1;
    
    c10000=a/10000; a%=10000;
    c5000=a/5000; a%=5000;
    c1000=a/1000; a%=1000;
    c500=a/500; a%=500;
    c100=a/100; a%=100;
    c50=a/50; a%=50;
    c10=a/10; a%=10;
    c5=a/5; a%=5;
    c1=a;

    printf("1万円札%d枚 5千円札%d枚  千円札%d枚\n",c10000,c5000,c1000);
    printf("500円玉%d枚 100円玉%d枚 50円玉%d枚\n",c500,c100,c50);
    printf("10円玉%d枚   5円玉%d枚    1円玉%d枚\n",c10,c5,c1);
}

実行結果

$ ./curr 77777
1万円札7枚 5千円札1枚  千円札2枚
500円玉1枚 100円玉2枚 50円玉1枚
10円玉2枚   5円玉1枚    1円玉2枚

python版

こちらは、python版です。chmod +x change.pyとして実行権を付け、./change.py nとして実行して下さい。

change.py
#!/usr/bin/python
import sys

def change(n):
    currency=[
              [ 10000,"1万円札 " ],[ 5000,"五千円札" ],[1000,"千円札  "],
              [ 500,  "五百円玉" ],[100,  "百円玉  "], [50,  "五十円玉"],
              [ 10,   "十円玉  "], [5,    "五円玉  "], [1,   "一円玉  "]
             ]

    for i in currency:
        print(f"{i[1]}:{n//i[0]}")
        n%=i[0]

if __name__=='__main__':
    change(int(sys.argv[1]))
    exit(0)

実行例

$ ./change.py 77777
1万円札 :7枚
五千円札:1枚
千円札  :2枚
五百円玉:1枚
百円玉  :2枚
五十円玉:1枚
十円玉  :2枚
五円玉  :1枚
一円玉  :2枚
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?