Go: package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
	"bufio"
)

var (
	sc = bufio.NewScanner(os.Stdin)
	buf = make([]byte, 1000)
)

func scanText() string{
	sc.Scan()
	return sc.Text()
}

func scanTextArray(n int) []string {
	out := make([]string, n)
	for i, v := range(strings.Split(scanText(), "")) {
		out[i] = v
	}
	return out
}

func scanInt() int {
	out, e := strconv.Atoi(scanText())
	if e != nil {
        panic(e)
    }
	return out
}

func scanIntArray(n int) []int {
	out := make([]int, n)
	for i, v := range(strings.Split(scanText(), " ")) {
		num, e := strconv.Atoi(v)
		if e != nil {
			panic(e)
		}
		out[i] = num
	}
	return out
}
//  template end

var (
	coins = [6]int{500, 100, 50, 10, 5, 1}
)

func f(data int) {
	ans := 0
	change := 1000 - data
	for _, v := range coins {
		for ;v <= change; {
			change -= v
			ans++
		}
	}
	fmt.Println(ans)
}

func main() {
	sc.Buffer(buf, 10000)
	for{
		data := scanInt()
		if data == 0 {
			break
		}
		f(data)
	}
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-8;


int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;
		int money = 1000 - n;
		int res = 100000;
		for(int i=0;i<=2;i++){
			for(int j=0;j<=10;j++){
				for(int k=0;k<=20;k++){
					for(int l=0;l<=100;l++){
						for(int m=0;m<=200;m++){
							if(500*i+100*j+50*k+10*l+5*m > money) continue;
							else res = min(i+j+k+l+m + (money - (500*i+100*j+50*k+10*l+5*m)),res);
						}
					}
				}
			}
		}

		printf("%d\n",res);
	}

}

C++: #include <bits/stdc++.h>
using namespace std;

#define llong long long
#define inf 999999999
#define mp make_pair
#define pb push_back
#define fn fill_n
typedef pair<int, int> P;
typedef pair<P, int> PP;
typedef pair<PP, int> PPP;
typedef struct data{
	int now;
	int cost;
}data;
int nh[4] = {1, 0, -1, 0};
int nw[4] = {0, 1, 0, -1};





int main(){
	int a, b;
	while(cin >> a, a){
		b = 1000 - a;
		int ret = 0;
		while(b != 0){
			if(b >= 500){
				ret+= b/500;
				b %= 500;
			}else if(b >= 100){
				ret += b/100;
				b %= 100;
			}else if(b >= 50){
				ret += b/50;
				b %= 50;
			}else if(b >= 10){
				ret += b/10;
				b %= 10;
			}else if(b >= 5){
				ret += b/5;
				b %= 5;
			}else{
				ret += b;
				b = 0;
			}
		}
		cout << ret << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>

using namespace std;

#define print(a) printf("%d\n", a)

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)

#define MAX(a,b) ((a)>(b)?(a):(b))


int main(){
	int co, mod, ch = 0;
	
	while (cin >> co != EOF){
		
		mod = 1000 - co;
		
		ch += mod/500;
		mod = mod%500;
		
		ch += mod/100;
		mod = mod%100;
		
		ch += mod/50;
		mod = mod%50;
		
		ch += mod/10;
		mod = mod%10;
		
		ch += mod/5;
		mod = mod%5;
		
		ch += mod;
		
		cout << ch << endl;
	}
	
	return 0;
}

C++: // JOI - Prelim  0521
// おつり
#include <bits/stdc++.h>

using namespace std;

using UInt = uint_fast64_t;

template <typename T>
using vec1 = vector<T>;

template <typename T>
T cin2var()
{
    T val;
    cin >> val;
    return val;
}

const vec1<UInt> coins{500, 100, 50, 10, 5, 1};

bool sub()
{
    const auto price(cin2var<UInt>());

    if (!price) return false;

    UInt pay          = 1000 - price;
    UInt pay_out_coin = 0;
    for (const auto& coin : coins) {
        pay_out_coin += pay / coin;
        pay = pay % coin;
    }
    cout << pay_out_coin << endl;
    return true;
}

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    while (sub());
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
        const vector<int> coin{500,100,50,10,5,1};
        while (1) {
                int n, z, c=0;
                cin >> n;
                if (n == 0) break;
                z = 1000 - n;
                rep(i, coin.size()) {
                        c += z / coin.at(i);
                        z =  z % coin.at(i);
                }
                cout << c << endl;
        }
        return 0;
}

C++: #include <bits/stdc++.h>//復習必須
#include<iostream>
#include<vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
#define P pair<int, int>
int main (){ 
     int a = 1;
    
    while(a != 0){
        cin >> a ;
       if(a == 0){
           break;
       }
        a = 1000 - a ;
        int num = 0 ;
        num += a / 500 ;
        a -= 500 * (a / 500) ;
        num += a / 100 ;
        a -= 100 * (a / 100) ;
        num += a / 50 ;
        a -= 50 * (a / 50) ;
        num += a / 10 ;
        a -= 10 * (a / 10) ;
        num += a / 5 ;
        a -= 5 * (a / 5) ;
        num += a ; 
        cout << num << endl;
        a = 1 ;
    }
    

}

C++: #include <iostream>
using namespace std;
int main(void)
{
	int a,b,c=0;
	cin >> a;
	while(a!=0){
	b=1000-a;
	c+=b/500;
	b=b%500;
	c+=b/100;
	b=b%100;
	c+=b/50;
	b=b%50;
	c+=b/10;
	b=b%10;
	c+=b/5;
	c+=b%5;
	cout << c << endl;
	c=0;
	cin >> a;
	}
	
	return 0;
}

C++: #include <cstdio>

int main (int argc, char const* argv[])
{
	
	int coin[6]={500,100,50,10,5,1};
	
	int a,r=0,m;
	
	while(true){
	
	scanf("%d",&m);
	if(m==0) break;
	m=1000-m;
	r=0;
	for(int i=0;i<6;i++){
		a=m/coin[i];
		r+=a;
		m-=(a*coin[i]);
	}
	
	
	printf("%d\n",r);
	
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define pq priority_queue
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int main() {
   int t[] = {500, 100, 50, 10, 5, 1};
   while (1) {
      int n, ans = 0;
      cin >> n;
      if (n == 0) break;
      n = 1000 - n;
      for (int i = 0; i < 6; i++) {
         ans += n / t[i];
         n %= t[i];
      }
      cout << ans << endl;
   }
   return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;

int main(){
    int mon,man;
    while(1){
        cin >> mon;
        if(mon == 0){
            break;
        }
        mon = 1000 - mon;
        man = 0;
        if(mon >= 500){
            man += mon / 500;
            mon = mon % 500;
        }
        if(mon >= 100){
            man += mon / 100;
            mon = mon % 100;
        }
        if(mon >= 50){
            man += mon / 50;
            mon = mon % 50;
        }
        if(mon >= 10){
            man += mon / 10;
            mon = mon % 10;
        }
        if(mon >= 5){
            man += mon / 5;
            mon = mon % 5;
        }
        man += mon;
        cout << man << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	
	while (1)
	{int a=1;
	int b;
	int c=0;
	int cz=0;

		cin >> a;
		if (a == 0)
		{
			break;
		}
		b = 1000-a;

		if (b >= 500)
		{
			cz = b / 500;
			c = c +cz ;
			b = b % 500;
		}
		 if (b >= 100)
		{
			cz = b /100;
			c = c + cz;
			b = b % 100;
			
		}
		 if (b >= 50)
		{
			cz = b / 50;
			c = c + cz;
			b = b % 50;
		 }
		 if (b >= 10)
		{
			cz = b / 10;
			c = c + cz;
			b = b % 10;
		}
		 if (b >= 5)
		{
			cz = b / 5;
			c = c + cz;
			b = b % 5;
		 }
		 if (b >= 1)
		{
			cz = b /1;
			c = c + cz;
			b = b % 1;
		}

		cout << c << endl;
	}
	return 0;
}