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++: #include <bits/stdc++.h>
using namespace std;

#define times(n, i) uptil(0, n, i)
#define upto(f, t, i) for(auto _##i = (t), i = decltype(_##i)(f); i <= _##i; i++)
#define uptil(f, t, i) for(auto _##i = (t), i = decltype(_##i)(f); i < _##i; i++)
#define downto(f, t, i) for(auto _##i = (t), i = decltype(_##i)(f); i >= _##i; i--)
#define downtil(f, t, i) for(auto _##i = (t), i = decltype(_##i)(f); i > _##i; i--)
#define unless(c) if(!(c))
#define until(c) while(!(c))
#define loop while(true)

#define all(v) v.begin(), v.end()
#define ln << "\n"

struct line{};

template<class T> istream& operator>>(istream& in, vector<T>& v) {
    times(v.capacity(), i) in >> v[i];
    return in;
}

template<class T> const ostream& operator<<(ostream& out, const vector<T>& v) {
    times(v.size(), i) out << (i ? " " : "") << v[i];
    return out;
}

template<class T, class...U> typename enable_if<is_same<T, line>::value, string>::type next(istream& in, const U&... args) {
    string s;
    getline(in, s, args...);
    return move(s);
}

template<class T, class...U> typename enable_if<!is_same<T, line>::value, T>::type next(istream& in, const U&... args) {
    T t(args...);
    in >> t;
    return move(t);
}
template<class T, class...U> auto next(const U&... args) -> decltype(next<T>(cin)) {
    return next<T>(cin, args...);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    loop {
        int N = 1000 - next<int>(), ans = 0;
        if(N == 1000) break;

        for(int coin : {500, 100, 50, 10, 5, 1}) {
            ans += N / coin;
            N %= coin;
        }

        cout << ans ln;
    }

    return 0;
}

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

int main() {
  int n;
  while(cin >> n&&n!=0){
  int ans=0;
  n=1000-n;
  vector<int> c={500,100,50,10,5,1};
  r(i,6){
    int t=n/c[i];
    n-=t*c[i];
    ans+=t;
  }
  cout<<ans<<endl;
  }
}

C++: #include<iostream>

using namespace std;

int main(void){
	
	int coin[10],price,sum;
	while(cin>>price,price){
		for(int i=0;i<10;i++){
			coin[i]=0;
		}
		sum=0;
		price=1000-price;
		if(price/500){
			coin[0]=(price/500);
			price=price-500*coin[0];
		}
		if(price/100){
			coin[1]=(price/100);
			price=price-100*coin[1];
		}
		if(price/50){
			coin[2]=(price/50);
			price=price-50*coin[2];
		}
		if(price/10){
			coin[3]=(price/10);
			price=price-10*coin[3];
		}
		if(price/5){
			coin[4]=(price/5);
			price=price-5*coin[4];
		}
		if(price/1){
			coin[5]=(price/1);
			price=price-coin[5];
		}
		for(int i=0;i<5;i++){
			sum=sum+coin[i];
		}
		cout<<sum<<endl;
	}
}

C++: #include <stdio.h>
#include <stdlib.h>
int main()
{
	int t=0,yen,put=0,coin[6]={500,100,50,10,5,1};
	scanf("%d",&yen);
	yen=abs(1000-yen);
	for(int i=0;i<6;i++,t=0)
	{
		t=yen/coin[i];
		put+=t;
		yen-=t*coin[i];
	}
	printf("%d\n",put);
	//Sleep(1000);
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	while(cin>>n,n){
		int ans=0;
		n=1000-n;
		ans+=n/500;n%=500;
		ans+=n/100;n%=100;
		ans+=n/50;n%=50;
		ans+=n/10;n%=10;
		ans+=n/5;n%=5;
		ans+=n;
		cout<<ans<<endl;
	}
}

C++: #include <stdio.h>

void main(){
	int okane;
	int oturi;
	int count = 0;
	int a[] = { 1, 5, 10, 50, 100, 500 };

	scanf("%d", &okane);
	oturi = 1000 - okane;

	for (int i = 5; i >= 0; i--){
		if (okane >= a[i]){
			okane = okane - a[i];
			count = count + 1;
		}
	}
	printf("%d\n",count);

}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

#define EPS (1e-7)
#define INF (1e15)
#define PI (acos(-1))
#define MOD 1000000007

#define REP(i,n) for(int i=0;i<n;i++)
#define REPS(i,f,n) for(int i=(f);i<(n);i++)
#define EACH(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define arrdump(n,a) for(int i=0;i<n;i++) {cout<<((i>0)? " ":"")<<a[i];}cout<<"\n";


typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy4[]={0, 0, 1, -1};
int dx4[]={1, -1, 0, 0};
int dx8[]={0, 0, 1, -1, 1, 1, -1, -1};
int dy8[]={1, -1, 0, 0, 1, -1, -1, 1};

//

int coins[] = {500, 100, 50, 10, 5, 1};

int main() {

    while(true) {
        int price; cin >> price;
        if (price == 0) break;
        int change = 1000-price;
        int cnt = 0;

        for (int c : coins) {
            int v = change / c;
            cnt += v;
            change -= (v*c);
        }

        cout << cnt << endl;

    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
int main(){
	int i=0,a,b,c,d,e,f,g,ans;
	while(i<5){
		cin>>a;
		if(a==0)break;
		b=1000-a;
		c=b%500;
		d=c%100;
		e=d%50;
		f=e%10;
		g=f%5;
		ans=b/500+c/100+d/50+e/10+f/5+g;
		cout<<ans<<endl;
		ans=0;
		i++;
	}
	return 0;
}

C++: #include <cstdio>

int main() {
	
	while(1){
		int num,n=0,i=0,a=0,b=0;
		scanf("%d",&num);
		if(num==0) break;
		n=1000-num;

	if (n>=500) {
		i++;
		n=(n-500);
	}
	if (n>=100){
		a=n/100;
		i=a+i;
		n=n%100;
	}
	if (n>=50){
		i++;
		n=(n-50);
	}
	if (n>=10){
		b=n/10;
		i=b+i;
		n=n%10;
	}
	if (n>=6){
		i++;
		n=(n-5);
	}
		i=(i+n);
	printf("%d\n",i);
	}
	
	return 0;
}

C++: #include <iostream>

using namespace std;

int main(){
  int n, now, count;
  while(1){
    cin >> n;
    now = 0;
    count = 0;
    if(n==0) break;
    
    while(1){
      if(1000-n-now >= 500){
        now += 500;
        count++;
      } else if (1000-n-now >= 100){
        now += 100;
        count++;
      } else if (1000-n-now >= 50){
        now += 50;
        count++;
      } else if (1000-n-now >= 10){
        now += 10;
        count++;
      } else if (1000-n-now >= 5){
        now += 5;
        count++;
      } else {
        now += 1;
        count++;
      }

      if(1000-n-now == 0) {
        cout << count << endl;
        break;
      }
    }
  }

  return 0;
}